#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2003
    Posts
    174
    Rep Power
    12

    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
  3. Commie Mutant Traitor
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Jun 2004
    Location
    Norcross, GA (again)
    Posts
    1,805
    Rep Power
    1570
    Iit is hard to tell, because we don't know the details of the HerdRation class, but in your :select statement, you don't actually reference a ration field, which (if I am reading this correctly) means that heard_ration.ration is a non-existent instance variable, which defaults to nil. I may be off on this, but that's the impression I get as to the cause of the problem.
    Rev First Speaker Schol-R-LEA;2 JAM LCF ELF KoR KCO BiWM TGIF
    #define KINSEY (rand() % 7) λ Scheme is the Red Pill
    Scheme in Short Understanding the C/C++ Preprocessor
    Taming Python A Highly Opinionated Review of Programming Languages for the Novice, v1.1

    FOR SALE: One ShapeSystem 2300 CMD, extensively modified for human use. Includes s/w for anthro, transgender, sex-appeal enhance, & Gillian Anderson and Jason D. Poit clone forms. Some wear. $4500 obo. tverres@et.ins.gov
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2003
    Posts
    174
    Rep Power
    12
    I am referencing a rations field in the select statement here:

    rations.ration_name
  6. #4
  7. Commie Mutant Traitor
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Jun 2004
    Location
    Norcross, GA (again)
    Posts
    1,805
    Rep Power
    1570
    Yes... but rations.ration_name and ration would not be not the same field, now would they? I have no intention of insulting your intelligence, but it is sometimes the most obvious points that escape us.

    Perhaps I am the one being dense, however. Is rations.ration_name meant to populate the ration instance variable of the HerdRation class in this query? We may need to see more of the HerdRation class to understand this part.

    Let's look at this in the other direction. Where is herd_ration.ration supposed to be getting set, and from what source?
    Last edited by Schol-R-LEA; July 6th, 2011 at 11:36 AM.
    Rev First Speaker Schol-R-LEA;2 JAM LCF ELF KoR KCO BiWM TGIF
    #define KINSEY (rand() % 7) λ Scheme is the Red Pill
    Scheme in Short Understanding the C/C++ Preprocessor
    Taming Python A Highly Opinionated Review of Programming Languages for the Novice, v1.1

    FOR SALE: One ShapeSystem 2300 CMD, extensively modified for human use. Includes s/w for anthro, transgender, sex-appeal enhance, & Gillian Anderson and Jason D. Poit clone forms. Some wear. $4500 obo. tverres@et.ins.gov
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2003
    Posts
    174
    Rep Power
    12
    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!

IMN logo majestic logo threadwatch logo seochat tools logo