# Who can fix my query syntax?

**URL:** <https://www.ruby-forum.com/t/who-can-fix-my-query-syntax/133722>\
**Category:** Rails\
**Created:** [April 2, 2008, 11:12pm UTC](https://www.ruby-forum.com/t/who-can-fix-my-query-syntax/133722 "2008-04-02T23:12:43Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![tharrisx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e19adc/32.png) [@tharrisx](https://www.ruby-forum.com/u/tharrisx)\
**Post date:** [April 2, 2008, 11:12pm UTC](https://www.ruby-forum.com/t/who-can-fix-my-query-syntax/133722/1 "2008-04-02T23:12:43Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![tharrisx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e19adc/32.png) [@tharrisx](https://www.ruby-forum.com/u/tharrisx)\
**Post date:** [April 3, 2008, 9:22am UTC](https://www.ruby-forum.com/t/who-can-fix-my-query-syntax/133722/2 "2008-04-03T09:22:04Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![tharrisx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e19adc/32.png) [@tharrisx](https://www.ruby-forum.com/u/tharrisx)\
**Post date:** [April 3, 2008, 9:58am UTC](https://www.ruby-forum.com/t/who-can-fix-my-query-syntax/133722/3 "2008-04-03T09:58:56Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![tharrisx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e19adc/32.png) [@tharrisx](https://www.ruby-forum.com/u/tharrisx)\
**Post date:** [April 3, 2008, 10:46am UTC](https://www.ruby-forum.com/t/who-can-fix-my-query-syntax/133722/4 "2008-04-03T10:46:31Z")

</div>

You can use the :conditions option:

has\_many :accepted\_groups, :through =\> :memberships, :source  
=\> :group,  
:conditions =\> [‘memberships.accepted =?’, true]

---

<div class="post-metadata">

**Author:** ![tharrisx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e19adc/32.png) [@tharrisx](https://www.ruby-forum.com/u/tharrisx)\
**Post date:** [April 3, 2008, 11:38am UTC](https://www.ruby-forum.com/t/who-can-fix-my-query-syntax/133722/5 "2008-04-03T11:38:31Z")

</div>

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!
