basically I''ve got a design which has a model describing, say a folder, and within each folder I associate some notes. I''ve built my models so that a folder can have many notes, and that a note belongs to a folder. Now, a folder can belongs to a person, and a person therefore has many folders. what I''m attempting to do is display all the folders for a given user, which is pretty simple as I can simply so folders = Person.find(1,:include=>[:folders]) however, what I also want to do is display the most recent note against each folder. Although I can do this by simply adding the notes into my include, folders = Person.find(1,:include=>[:folders=>:notes]) I''d really perfer not to load each and every note assigned to each and every folder. I''ve been playing about for a while now trying to get active record to load my assoicates but only include the first note for each folder. can anybody help? maybe it''s not even possible? Perhaps I need to change my approach? Any help/advice would be greatly appreciated/welcome. thanks --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 17 Oct 2008, at 11:27, hiddenhippo wrote:> folders = Person.find(1,:include=>[:folders]) > > however, what I also want to do is display the most recent note > against each folder. Although I can do this by simply adding the > notes into my include, > > folders = Person.find(1,:include=>[:folders=>:notes]) > > I''d really perfer not to load each and every note assigned to each > and every folder. I''ve been playing about for a while now trying to > get active record to load my assoicates but only include the first > note for each folder. can anybody help? maybe it''s not even > possible? Perhaps I need to change my approach? Any help/advice > would be greatly appreciated/welcome. >:include can''t do that - it''s fundamentally hard from it''s point of view to only fetch the first record of some collection Fred> thanks > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
hiddenhippo wrote:> basically I''ve got a design which has a model describing, say a > folder, and within each folder I associate some notes. I''ve built my > models so that a folder can have many notes, and that a note belongs > to a folder. Now, a folder can belongs to a person, and a person > therefore has many folders. what I''m attempting to do is display all > the folders for a given user, which is pretty simple as I can simply > so > > folders = Person.find(1,:include=>[:folders]) > > however, what I also want to do is display the most recent note > against each folder. Although I can do this by simply adding the > notes into my include, > > folders = Person.find(1,:include=>[:folders=>:notes]) > > I''d really perfer not to load each and every note assigned to each > and every folder. I''ve been playing about for a while now trying to > get active record to load my assoicates but only include the first > note for each folder. can anybody help? maybe it''s not even > possible? Perhaps I need to change my approach? Any help/advice > would be greatly appreciated/welcome.This may work: class Folder < ActiveRecord::Base has_one :most_recent_note, :class_name => ''Note'', :foreign_key => :note_id, :conditions => ''notes.created_at = (select max(created_at) from notes)'' end folders = Person.find(1, :include => {:folders => :most_recent_note}) -- Rails Wheels - Find Plugins, List & Sell Plugins - http://railswheels.com --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---