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:
memberships:
- id
- user_id
- group_id
- accepted (true/false)
groups:
=> 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
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])
Craig
Craig W. 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])
Craig
Hey 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
You can use the :conditions option:
has_many :accepted_groups, :through => :memberships, :source
=> :group,
:conditions => [‘memberships.accepted =?’, true]
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!