Hi Let''s say I have three models, Company, User and Entry (and a join model EntryParticipants). Company has many Users, a User has many Entries (Entries created by a User). Also, there is another many-to-many relationship between User and Entry (a User can participate in many entries and an Entry can have many participants). So, now I want to fetch all Entries that a User has created OR is participating in. I''m a bit stuck though... user.entries => All entries created by this user user.participated_entries => All participated entries But now I want to get them all in one query. The code below is working, but is there a more efficient way to do it? scope.includes(:entry_participants).where("(entry_participants.participated_entry_id = entries.id) OR entries.user_id = ?", user.id) -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/juJW42o7sd8J. For more options, visit https://groups.google.com/groups/opt_out.
Why bother and not doing: (user.entries + user.participated_entries).uniq I wonder if a complicated single query is must faster.... Op woensdag 20 februari 2013 20:20:46 UTC+1 schreef Linus Pettersson het volgende:> > Hi > > Let''s say I have three models, Company, User and Entry (and a join model > EntryParticipants). > > Company has many Users, a User has many Entries (Entries created by a > User). > Also, there is another many-to-many relationship between User and Entry (a > User can participate in many entries and an Entry can have many > participants). > > So, now I want to fetch all Entries that a User has created OR is > participating in. I''m a bit stuck though... > > user.entries => All entries created by this user > user.participated_entries => All participated entries > > But now I want to get them all in one query. The code below is working, > but is there a more efficient way to do it? > > scope.includes(:entry_participants).where("(entry_participants.participated_entry_id > = entries.id) OR entries.user_id = ?", user.id) >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/hehyXdhYdQAJ. For more options, visit https://groups.google.com/groups/opt_out.
On Wednesday, 20 February 2013 14:20:46 UTC-5, Linus Pettersson wrote:> > Hi > > Let''s say I have three models, Company, User and Entry (and a join model > EntryParticipants). > > Company has many Users, a User has many Entries (Entries created by a > User). > Also, there is another many-to-many relationship between User and Entry (a > User can participate in many entries and an Entry can have many > participants). > > So, now I want to fetch all Entries that a User has created OR is > participating in. I''m a bit stuck though... > > user.entries => All entries created by this user > user.participated_entries => All participated entries > > But now I want to get them all in one query. The code below is working, > but is there a more efficient way to do it? > > scope.includes(:entry_participants).where("(entry_participants.participated_entry_id > = entries.id) OR entries.user_id = ?", user.id) >If it makes sense for your domain model, adding some logic to enforce that the user who created the Entry is always also a participant makes this trivial. --Matt Jones -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/UdOD8j6fRScJ. For more options, visit https://groups.google.com/groups/opt_out.