# Conditions =\> Or instead of AND

**URL:** <https://www.ruby-forum.com/t/conditions-or-instead-of-and/117175>\
**Category:** Rails\
**Created:** [October 14, 2007, 11:43am UTC](https://www.ruby-forum.com/t/conditions-or-instead-of-and/117175 "2007-10-14T11:43:49Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jamal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/35a633/32.png) [@jamal](https://www.ruby-forum.com/u/jamal)\
**Post date:** [October 14, 2007, 11:43am UTC](https://www.ruby-forum.com/t/conditions-or-instead-of-and/117175/1 "2007-10-14T11:43:49Z")

</div>

Hello,

I have in my User model.

belongs :groups

I want to put condition on it so it end of like this

SELECT \* FROM groups WHERE (groups.user\_id = 2 OR (groups.user\_id is  
NULL)

…so I thought this would do that

belongs :groups, :conditions =\> [“groups.user\_id is NULL”]

…but this add AND between the conditions and not OR?

I hope someone can tell me how to change the AND to OR in the belongs  
and thanks for your help:)

---

<div class="post-metadata">

**Author:** ![jamal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/35a633/32.png) [@jamal](https://www.ruby-forum.com/u/jamal)\
**Post date:** [October 14, 2007, 7:22pm UTC](https://www.ruby-forum.com/t/conditions-or-instead-of-and/117175/2 "2007-10-14T19:22:53Z")

</div>

> ## Group.find(:all, :conditions =\> [“user\_id IS NULL OR user\_id = ?”, user.id])
> 
> Rick DeNatale
> 
> My blog on Ruby  
> [http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)

Thanks for your answer Rick.

I just ended up doing what you just did in the end of your post.

Group.find(:all, :conditions =\> [“user\_id IS NULL OR user\_id = ?”,  
user.id]

---

<div class="post-metadata">

**Author:** ![jamal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/35a633/32.png) [@jamal](https://www.ruby-forum.com/u/jamal)\
**Post date:** [October 14, 2007, 5:21pm UTC](https://www.ruby-forum.com/t/conditions-or-instead-of-and/117175/3 "2007-10-14T17:21:41Z")

</div>

On 10/14/07, Jamal S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hello,
> 
> I have in my User model.

Let’s step back a bit.

> belongs :groups

I think you mean  
belongs\_to :group

This means that AR is expecting your User table to have a foreign key  
to the Group table with the name group\_id

With this association a user is associated with a single group.

> I want to put condition on it so it end of like this
> 
> SELECT \* FROM groups WHERE (groups.user\_id = 2 OR (groups.user\_id is  
> NULL)
> 
> …so I thought this would do that
> 
> belongs :groups, :conditions =\> [“groups.user\_id is NULL”]

The condition on an association declaration adds additional  
restrictions on what will be returned on the query. with a belongs\_to  
:group association, you get a group (not groups) method which returns  
at most one group. If effectively does Group.find(group\_id).

It seems like you’re modeling this wrong.

What are the real-world relationships between user and groups?

If a group can have more than one user (seems likely) then you want a  
has\_many relationship back to user. This still would not requre a  
user\_id column in group.

If, on the other hand, you want some groups to belong to a single user  
then group should have a belongs\_to :user, and a user\_id column

If the user table has a user\_id column then to find groups which  
belong to either a particular user or no-one you can do something  
like:

## Group.find(:all, :conditions =\> [“user\_id IS NULL OR user\_id = ?”, user.id])

Rick DeNatale

My blog on Ruby  
[http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)
