# Need help with find

**URL:** <https://www.ruby-forum.com/t/need-help-with-find/186231>\
**Category:** Rails\
**Created:** [April 2, 2010, 4:23pm UTC](https://www.ruby-forum.com/t/need-help-with-find/186231 "2010-04-02T16:23:01Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![RVRoadie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/4bbf92/32.png) [@RVRoadie](https://www.ruby-forum.com/u/RVRoadie)\
**Post date:** [April 2, 2010, 4:23pm UTC](https://www.ruby-forum.com/t/need-help-with-find/186231/1 "2010-04-02T16:23:01Z")

</div>

Kind of new at this and have been unable to find an example of what I  
want to do.

I have three models, people, clubs and members.

@members = @club.members.all --\> finds all club members

How do I write a find of all people that are not members of @club

Thanks

---

<div class="post-metadata">

**Author:** ![RVRoadie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/4bbf92/32.png) [@RVRoadie](https://www.ruby-forum.com/u/RVRoadie)\
**Post date:** [April 3, 2010, 4:26am UTC](https://www.ruby-forum.com/t/need-help-with-find/186231/2 "2010-04-03T04:26:36Z")

</div>

Finally figured out something that worked.

@club = Club.find(params[:club\_id])  
ids = @club.members.connection.select\_values(‘SELECT person\_id FROM  
members’)  
@people = Person.all(:order =\> ‘name\_sort’,  
:conditions =\> [“id NOT IN (?)”,  
ids])

Any comments or better methods.

---

<div class="post-metadata">

**Author:** ![RVRoadie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/4bbf92/32.png) [@RVRoadie](https://www.ruby-forum.com/u/RVRoadie)\
**Post date:** [April 3, 2010, 9:32am UTC](https://www.ruby-forum.com/t/need-help-with-find/186231/3 "2010-04-03T09:32:38Z")

</div>

On 2 April 2010 15:21, RVRoadie [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Kind of new at this and have been unable to find an example of what I  
> want to do.
> 
> I have three models, people, clubs and members.
> 
> @members = @club.members.all → finds all club members
> 
> How do I write a find of all people that are not members of @club

What are the relationships between the models (has\_many, belongs\_to and  
so on)?

Colin

---

<div class="post-metadata">

**Author:** ![RVRoadie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/4bbf92/32.png) [@RVRoadie](https://www.ruby-forum.com/u/RVRoadie)\
**Post date:** [April 3, 2010, 4:33pm UTC](https://www.ruby-forum.com/t/need-help-with-find/186231/4 "2010-04-03T16:33:25Z")

</div>

Club has many Members  
Person has many Members  
Members belong to Club, Person

---

<div class="post-metadata">

**Author:** ![RVRoadie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/4bbf92/32.png) [@RVRoadie](https://www.ruby-forum.com/u/RVRoadie)\
**Post date:** [April 3, 2010, 5:10pm UTC](https://www.ruby-forum.com/t/need-help-with-find/186231/5 "2010-04-03T17:10:18Z")

</div>

On 3 April 2010 15:32, RVRoadie [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Club has many Members  
> Person has many Members

That’s novel.  
Possibly memberships would be a better name, or am I misunderstanding  
what this table is for?

> Members belong to Club, Person

You seem to have snipped the original question. I will have to get it  
back from the earlier post.  
On 2 April 2010 15:21, RVRoadie [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Kind of new at this and have been unable to find an example of what I  
> want to do.
> 
> I have three models, people, clubs and members.
> 
> @members = @club.members.all → finds all club members

In fact you can just use @club.members to give you all the members of  
@club. Are you not more likely to want all the people that are  
members of the club though?

If you also say club has\_many people through memberships then you can  
also @club.people to give you all the actual people.

> How do I write a find of all people that are not members of @club

If you further have person has\_many clubs through memberships then  
person.clubs will give you all his clubs and to get the people who are  
not members of @club you can do something like

person.find( :all, :include =\> :clubs, :conditions =\> [‘club.id \<\> ?’,  
@club.id] )

That might not be quite right, but something like that.
