# Find with has\_many

**URL:** <https://www.ruby-forum.com/t/find-with-has-many/77132>\
**Category:** Rails\
**Created:** [November 24, 2006, 4:12pm UTC](https://www.ruby-forum.com/t/find-with-has-many/77132 "2006-11-24T16:12:24Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mjgp2](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/eb8c5e/32.png) [@mjgp2](https://www.ruby-forum.com/u/mjgp2)\
**Post date:** [November 24, 2006, 4:12pm UTC](https://www.ruby-forum.com/t/find-with-has-many/77132/1 "2006-11-24T16:12:24Z")

</div>

I have a model Person and one Organisation. A Person can belong to any  
number of Organisations and an Organisation can have any number of  
Persons attributed to it. As below:

====  
class Person \< ActiveRecord::Base  
has\_many :person\_organisations, :dependent =\> true  
has\_many :organisations, :through =\> :person\_organisations  
end

# class Organisation \< ActiveRecord::Base has\_many :person\_organisations, :dependent =\> true has\_many :persons, :through =\> :person\_organisations end

I’m writing an action on my Person controller called  
‘list\_by\_organisation’ which will return all the contacts from a  
specified organisation. How do I achieve this?

Do I do something like the following? But what goes in the condition?

# ==== def list\_by\_organisation @person\_pages, @persons = paginate(:persons, :per\_page =\> 20, :order =\> ‘id’, :conditions =\> [???]) render\_action ‘list’ end

If I’m doing this in completely the wrong way then please do let me  
know.

Thanks for any suggestions.

---

<div class="post-metadata">

**Author:** ![mjgp2](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/eb8c5e/32.png) [@mjgp2](https://www.ruby-forum.com/u/mjgp2)\
**Post date:** [November 24, 2006, 8:44pm UTC](https://www.ruby-forum.com/t/find-with-has-many/77132/2 "2006-11-24T20:44:38Z")

</div>

Matthew Planchant wrote:

> class Organisation \< ActiveRecord::Base
> 
> Thanks for any suggestions.

You need either has\_and\_belongs\_to\_many (if the join table has no  
attributes), in which case you don’t need :through (AR does all that for  
you), or if the join table has attributes (e.g. date of joining) you  
need to declare that as a model (in which case I’d prob just call it  
membership).

In your Person controller you could do something like @organisations =  
Organisation.find(:all, :include =\> :persons) if you want all the  
organisations, and then in your view iterate through them:

\<% @organisations.each do |organisation| %\>

# \<%= h(organisation.title) %\>
  
organisation.persons.each do |person|  

\<%= h(person.name) %\>

  
\<% end %\>  
\<% end %\>

or if you just want members of a specific organisation (identified by,  
say, params[:organisation\_id])  
@persons = Organisation.find(params[:organisation\_id]).persons

Or something like that.

HTH  
CT  
p.s. I think rails by default uses people as the plural of persons (so  
should be has\_many :people, not has\_many :persons, ditto all the other  
persons)

---

<div class="post-metadata">

**Author:** ![mjgp2](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/eb8c5e/32.png) [@mjgp2](https://www.ruby-forum.com/u/mjgp2)\
**Post date:** [November 27, 2006, 10:53am UTC](https://www.ruby-forum.com/t/find-with-has-many/77132/3 "2006-11-27T10:53:51Z")

</div>

> You need either has\_and\_belongs\_to\_many (if the join table has no  
> attributes), in which case you don’t need :through (AR does all that for  
> you), or if the join table has attributes (e.g. date of joining) you  
> need to declare that as a model (in which case I’d prob just call it  
> membership).

Yep.

> In your Person controller you could do something like @organisations =  
> Organisation.find(:all, :include =\> :persons) if you want all the  
> organisations, and then in your view iterate through them:
> 
> \<% @organisations.each do |organisation| %\>
> 
> # \<%= h(organisation.title) %\>
>   
> organisation.persons.each do |person|  
> 
> \<%= h(person.name) %\>
> 
>   
> \<% end %\>  
> \<% end %\>
> 
> or if you just want members of a specific organisation (identified by,  
> say, params[:organisation\_id])  
> @persons = Organisation.find(params[:organisation\_id]).persons
> 
> Or something like that.

OK. Thanks.

So what is wrong with:

@person\_pages, @people = paginate(:people, :per\_page =\> 20, :order =\>  
‘id’, :include =\> :organisations, :conditions =\> [‘organisations.id=?’,  
params[:id]])

> p.s. I think rails by default uses people as the plural of persons (so  
> should be has\_many :people, not has\_many :persons, ditto all the other  
> persons)

Yep I’ve realised this. The above is just an example though.

---

<div class="post-metadata">

**Author:** ![mjgp2](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/eb8c5e/32.png) [@mjgp2](https://www.ruby-forum.com/u/mjgp2)\
**Post date:** [November 27, 2006, 11:12am UTC](https://www.ruby-forum.com/t/find-with-has-many/77132/4 "2006-11-27T11:12:27Z")

</div>

Chris T wrote:

> or if you just want members of a specific organisation (identified by,  
> say, params[:organisation\_id])  
> @persons = Organisation.find(params[:organisation\_id]).persons

That worked many thanks!
