OK, bare (bear?) with me here :) I have three tables... users, groups and meetings users habtm groups via join table groups_users users habtm meetings via join_table attendees groups habtm users via join table groups_users groups habtm meetings via groups_meetings meetings habtm users via attendees meetings habtm groups via groups_meetings The attendees join table provides some rich assoociation attributes, such as attendance status, special requirements, that sort of thing So, we have a triangular set of tables each with many-many relationships. The users-meetings association is needed due to the extra user specific information it provides. Anyway... I''m having some difficulties grasping how I might undertake eager loading of associations in a couple of scenarios. For instance, I want to get a list of meetings for a user... easy, I simply call user.meetings and bingo! There it is. BUT, iterating through those meetings and retrieving the attendee list for each one results in a LOT of queries. I''ve looked at eager loading but can''t really work out how to apply it here. I''ve checked the documentation and the wiki and kind of understand I should do something like: Meeting.find(:all, :include => :users) But what I don''t understand is what conditions I need to provide to limit it to just this users'' meetings. I would appreciate any help/advice/word of wisdom muchly. Ta, Steve
On 4 Oct 2005, at 16:43, Steven Mohapi-Banks wrote:> OK, bare (bear?) with me here :)Well, I''ll bear. But whatever floats your boat. ;)> Meeting.find(:all, :include => :users) > > But what I don''t understand is what conditions I need to provide to > limit it to just this users'' meetings.You could build an ''in'' condition. Off the top of my head: user_meeting_ids = user.meetings.map {|m| m.id } Meeting.find(:all, :include => :users, :conditions => [''meetings.id in (?)'', user_meeting_ids.join('','')]) Or somesuch. Maybe there''s a neater way of doing this? Hope this helps. Mike
Cheers Mike, Just a quick note to say that the call to join in the conditions statement is unnecessary. Seems to have done the job though. Ace! Steve On 4 Oct 2005, at 17:18, Michael Houghton wrote:> > On 4 Oct 2005, at 16:43, Steven Mohapi-Banks wrote: > > >> OK, bare (bear?) with me here :) >> > > Well, I''ll bear. But whatever floats your boat. ;) > > >> Meeting.find(:all, :include => :users) >> >> But what I don''t understand is what conditions I need to provide >> to limit it to just this users'' meetings. >> > > You could build an ''in'' condition. Off the top of my head: > > user_meeting_ids = user.meetings.map {|m| m.id } > > Meeting.find(:all, :include => :users, > :conditions => [''meetings.id in (?)'', user_meeting_ids.join('','')]) > > Or somesuch. Maybe there''s a neater way of doing this? > > Hope this helps. > > Mike > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >