SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Zealot
    Join Date
    Jul 2004
    Location
    NC
    Posts
    194
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    looping through results of complex query

    Hello,
    I have a postgresql join query that returns all the correct values on command line but when I try to loop through the results in a view, I can't access all the fields.

    Specifically, I'm trying to display the fields in the rations table and can't seem to print them out to save my life.

    Here's the query:

    Code:
    @herd_rations_during_season = HerdRation.find(:all, 
            :joins => ["INNER JOIN seasons ON seasons.herd_id = herd_rations.herd_id AND seasons.season_start_date >= herd_rations.grazing_begun AND herd_rations.grazing_end <= seasons.season_end_date INNER JOIN rations ON rations.id = herd_rations.ration_id"],
              :select=>'DISTINCT ON (herd_rations.id) herd_rations.id, herd_rations.grazing_begun, herd_rations.grazing_end, seasons.season_name, seasons.season_start_date, seasons.season_end_date, rations.ration_name', 
              :conditions => ["herd_rations.herd_id=?", params[:id]])
    
    @herd_rations_during_season.each do |herd_ration|
    	puts("----herd_ration: #{herd_ration.ration}")         	#prints nil
    	puts("----grazing_begun: #{herd_ration.grazing_begun}")	#good
    	puts("---- grazing_end: #{herd_ration.grazing_end}")  	#good
    end
    Thanks for any help!

  2. #2
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    You do not seem to be selecting that column (:select=>...). You can't print columns you don't ask the database for.

  3. #3
    SitePoint Zealot
    Join Date
    Jul 2004
    Location
    NC
    Posts
    194
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I am referencing a rations field in the select statement here:

    rations.ration_name

  4. #4
    SitePoint Zealot
    Join Date
    Jul 2004
    Location
    NC
    Posts
    194
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    solved the problem and feel a bit stupid but, at the same time not.

    I accessed ration.ration_name by simply doing this in my loop:

    Code:
    herd_ration.ration_name
    The part I'm not understanding, is that when I used inspect in the loop, it did not display any reference to to ration.ration_name:

    Code:
    @herd_rations_during_season.each do |herd_ration|
    	puts("----herd_ration: #{herd_ration.inspect}")   #doesn't display ration_name anywhere
    end
    thanks for all reply and great weekend to all!


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
  •