SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Zealot ricklach's Avatar
    Join Date
    Nov 2004
    Location
    Victoria BC
    Posts
    116
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Mowing the Grass

    I am at the grass mowing stage because I have not been able to solve my current coding problem for the past week. I am getting closer to a solution but just can't crack the code and think I need some basic assistance to accomplish a number of outstanding issues. This bit of code works like a charm:
    Code:
    def search
    	
    	@search_result = Voter.ez_find(:all)  do |voters|
    		if params[:voter][:gender] != 'U'
    			if params[:voter][:gender] == 'M'
    				voters.gender == 'M'
    			else
    				voters.gender == 'F'
    			end
    		end
    		voters.lastname =~ params[:voter][:lastname] if params[:voter][:lastname]
    		voters.firstname =~ params[:voter][:firstname] if params[:voter][:firstname]
    		voters.ph_home == params[:voter][:ph_home] if (params[:voter][:ph_home] != '000-000-0000' && params[:voter][:ph_home] != '')
    		#residences do
    		#	residences.number == params[:residence][:number] if params[:residence][:number]
    		#	residences.streetname == params[:residence][:streetname] if params[:residence][:streetname]
    		#	residences.streettype == params[:residence][:streettype] if params[:residence][:streettype]
    		#	residences.city == params[:residence][:city] if params[:residence][:city]
    		#	#residences.postalcode == params[:residence][:postalcode] if params[:residence][:postalcode]
    		#end
    	end
    	
      end
    This bit of code breaks the previous code and issues an error (undefined method `lastname' for #<Array:0x3639710>):
    Code:
    def search
    	
    	@search_result = Voter.ez_find(:all, :include => :residence)  do |voters|
    		if params[:voter][:gender] != 'U'
    			if params[:voter][:gender] == 'M'
    				voters.gender == 'M'
    			else
    				voters.gender == 'F'
    			end
    		end
    		voters.lastname =~ params[:voter][:lastname] if params[:voter][:lastname]
    		voters.firstname =~ params[:voter][:firstname] if params[:voter][:firstname]
    		voters.ph_home == params[:voter][:ph_home] if (params[:voter][:ph_home] != '000-000-0000' && params[:voter][:ph_home] != '')
    		#residences do
    		#	residences.number == params[:residence][:number] if params[:residence][:number]
    		#	residences.streetname == params[:residence][:streetname] if params[:residence][:streetname]
    		#	residences.streettype == params[:residence][:streettype] if params[:residence][:streettype]
    		#	residences.city == params[:residence][:city] if params[:residence][:city]
    		#	#residences.postalcode == params[:residence][:postalcode] if params[:residence][:postalcode]
    		#end
    	end
    	
      end
    in the voter model I have: belongs_to :residence ; and in the residence model I have: has_many :voter.

    So I am not sure why the method is failing. Any help in decyphering this puzzle would be most appreciated.

    Rick
    Ruby, Ruby when will you be mine

  2. #2
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm not sure what the error is, but voters seems to be an array rather than a voter. Put a breakpoint at the first line of the ez_find block, and start the breakpoint script. The go to the page (with your browser). The breakpointer script opens an irb session in your code. So you'll be able to inspect the voters variable, and try other things out.

  3. #3
    SitePoint Zealot ricklach's Avatar
    Join Date
    Nov 2004
    Location
    Victoria BC
    Posts
    116
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well I did not have much success with that suggestion from the point of view of not knowing what to do next. I get the error: "Undefinded local variable or method for residence." Ok so if we accept that residence is an undefined method how do I go about defining it. For example, the search method is in controller - search_popups_controller.rb. Do I have to define the voter/residence in the model or is it sufficient to define it in the voter model (where it is already defined)? Do I have the definition correct in the voter model - the voters table contains the foreign key residence_id and the residences table has the id field. So is it voter belongs_to :residences and residence has_one :voter or voter has_one :residence and residence belongs_to :voters? The problem is clearly with the :include => :residence (or is it plural residences?) because if I remove it the method works (to a point). Lost without a compass,

    Rick
    Ruby, Ruby when will you be mine

  4. #4
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You should put the belongs_to in the model that has the id in the table. So if it is:

    table voters:
    id
    ...
    residence_id

    You should do:

    voter belongs_to :residence
    redidence has_one :voter

    Or am I wrong now?

    Maybe include needs an array?

    :include => [:residence]

    (not residences, probably)

  5. #5
    SitePoint Zealot
    Join Date
    Jul 2004
    Location
    Oklahoma
    Posts
    119
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    def search
    	
    	@search_result = Voter.ez_find(:all, :include => :residence)  do |voters|
    		if params[:voter][:gender] != 'U'
    			if params[:voter][:gender] == 'M'
    Code:
    				voters.gender == 'M'
    This line is not making an assignment, it should be:
    Code:
    voters.gender = 'M'
    Code:
    			else
    				voters.gender == 'F'
    			end
    Same here still no assignement, use an = not a ==
    Code:
    		end
    		voters.lastname =~ params[:voter][:lastname] if params[:voter][:lastname]
    What exactly are you trying to do here? Because what's actually being done looks something like this (in pseudo code)
    Code:
    if params[lastname] is posted then
      if voters.lastname regex matches params[lastname]
      end if
    end if
    the =~ is a regex match operator, it's typically used like this:
    Code:
    test = 'asdf'
    if test =~ /^a/ # should return a "true" value (anything other than nil or false)
      #do something when a matches
    end
    It does not do an assignment. I think you need to do some really serious playing with JUST Ruby before diving head first into Rails because you're missing some fairly basic concepts. I highly recommend http://www.manning.com/black/ this book if you plan on using Rails.

  6. #6
    SitePoint Zealot ricklach's Avatar
    Join Date
    Nov 2004
    Location
    Victoria BC
    Posts
    116
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Regarding the last reply, I picked up the error you pointed out after I posted the code. I am aware of the assignments. The class/method I am using creates a conditions where clause (ez_find) that uses the symbology I have posted. So =~ means like when translated in the method. And so on. However, there appears to be a problem with the class I am using so I will wait until that is fixed since it appears that it is the source of the problem and not my code (at least according to the developer). Thanks everyone for the suggestions.

    Rick
    Ruby, Ruby when will you be mine


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •