SitePoint Sponsor |
|
User Tag List
Results 1 to 4 of 4
Thread: Sorty_by in nested tables
-
Jan 13, 2009, 14:30 #1
- Join Date
- Nov 2008
- Posts
- 8
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Sorty_by in nested tables
I need to do an : order => on an attribute in a double nested table, but I can't get it to work.
class CabinetNominee
belongs_to :cabinet_pick
end
class CabinetPick
belongs_to :cabinet_position
end
class CabinetNominee
end
In my views, I can loop through
@cabinet_nominee.each do |c|
c.cabinet_pick.cabinet_position.name
end
just fine. But, in my controller, I need to order my @cabinet_nominee collection by
cabinet_pick.cabinet_position.name DESC, and I can't figure out how. I have used :through and :join and .sort_by and on and on. Any one have any ideas?
Have I provided enough here?
Thanks!
-
Jan 13, 2009, 16:26 #2
- Join Date
- Feb 2006
- Location
- Worcs. UK
- Posts
- 404
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
If all you were doing was picking out the names and then ordering them, you could do this:
Code:names = @cabinet_nominees.collect{|c| c.cabinet_pick.cabinet_position.name} for name in names.sort #output name end
Can you post your table fields and what information you want to display? It should then be possible to suggest a single SQL call that will gather all the information you want.
-
Jan 13, 2009, 16:55 #3
- Join Date
- Nov 2008
- Posts
- 8
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Thanks for the reply. The majority of what I need to display comes from :cabinet_nominee table. I only need to display the attributes full_name from :cabinet_pick and name from :cabinet_position. Again, all of this works fine in the view. I would just like to order my @cabinet_nominee by cabinet_position.name.
I tried @cabinet_nominee.sort_by { |za| za[:cabinet_nominee][:cabinet_pick][:cabinet_position][:name] } but that didn't work.
I guess I could try going the other way, and setting @cabinet_nominee = CabinetPosition.find ( : all, {:include ... }, : order => 'name') and see what happens.....
-
Jan 14, 2009, 06:17 #4
- Join Date
- Feb 2006
- Location
- Worcs. UK
- Posts
- 404
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Have you tried:
Code:@cabinet_nominee.sort_by { |za| za.cabinet_pick.cabinet_position.name }
Bookmarks