Hi there, is there somebody who could help me out with the correct query syntax? The situation is the following: MODELS: ====== user has_many :memberships user has_many :groups, :through => :memberships, :source => :group TABLES: ====== users: - id memberships: - id - user_id - group_id - accepted (true/false) groups: - id => What I need is the collection of groups ("@groups") for a user, but only if the "memberships" have "true" in the column "accepted". I started with something like this (but don''t know how to fix the conditions part): @groups = @user.groups.find(:all, :conditions => { "the memberships must have ''true'' in the ''accepted'' column" }) Thank you very much for any help with this! Tom -- Posted via http://www.ruby-forum.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 -~----------~----~----~----~------~----~------~--~---
On Wed, 2008-04-02 at 23:12 +0200, Tom Ha wrote:> Hi there, > > is there somebody who could help me out with the correct query syntax? > > The situation is the following: > > MODELS: > ======> > user has_many :memberships > > user has_many :groups, > :through => :memberships, > :source => :group > > TABLES: > ======> > users: > - id > > memberships: > - id > - user_id > - group_id > - accepted (true/false) > > groups: > - id > > => What I need is the collection of groups ("@groups") for a user, but > only if the "memberships" have "true" in the column "accepted". > > I started with something like this (but don''t know how to fix the > conditions part): > > @groups = @user.groups.find(:all, :conditions => { "the memberships must > have ''true'' in the ''accepted'' column" })---- @user = User.find($SomeIdNumber) @groups = Group.find(:all, :conditions => ["accepted IS TRUE AND user_id = ?", @user]) Craig --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Craig White wrote:> On Wed, 2008-04-02 at 23:12 +0200, Tom Ha wrote: >> >> memberships: >> >> I started with something like this (but don''t know how to fix the >> conditions part): >> >> @groups = @user.groups.find(:all, :conditions => { "the memberships must >> have ''true'' in the ''accepted'' column" }) > ---- > @user = User.find($SomeIdNumber) > @groups = Group.find(:all, :conditions => ["accepted IS TRUE AND user_id > = ?", @user]) > > CraigHey Craig, thanks for your answer! One question back though: In your ''conditions'' statement: doesn''t "accepted" refer to the ''group'' table? I''m asking because the tricky part in my case is: "accepted" refers to/exists only in the ''membership'' table... (see above: the model is: user has_many :groups, :through => :memberships, :source => :group) Tom -- Posted via http://www.ruby-forum.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 -~----------~----~----~----~------~----~------~--~---
You can use the :conditions option: has_many :accepted_groups, :through => :memberships, :source => :group, :conditions => [''memberships.accepted =?'', true] On Apr 3, 5:12 am, Tom Ha <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Hi there, > > is there somebody who could help me out with the correct query syntax? > > The situation is the following: > > MODELS: > ======> > user has_many :memberships > > user has_many :groups, > :through => :memberships, > :source => :group > > TABLES: > ======> > users: > - id > > memberships: > - id > - user_id > - group_id > - accepted (true/false) > > groups: > - id > > => What I need is the collection of groups ("@groups") for a user, but > only if the "memberships" have "true" in the column "accepted". > > I started with something like this (but don''t know how to fix the > conditions part): > > @groups = @user.groups.find(:all, :conditions => { "the memberships must > have ''true'' in the ''accepted'' column" }) > > Thank you very much for any help with this! > Tom > -- > Posted viahttp://www.ruby-forum.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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
ashchan wrote:> You can use the :conditions option: > > has_many :accepted_groups, :through => :memberships, :source > => :group, > :conditions => [''memberships.accepted =?'', true]Great hint, thanks a bunch, ashchan! -- Posted via http://www.ruby-forum.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 -~----------~----~----~----~------~----~------~--~---