# Belongs\_to has\_many listing

**URL:** <https://www.ruby-forum.com/t/belongs-to-has-many-listing/124438>\
**Category:** Rails\
**Created:** [December 28, 2007, 5:41pm UTC](https://www.ruby-forum.com/t/belongs-to-has-many-listing/124438 "2007-12-28T17:41:41Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![hiddenhippo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/65b543/32.png) [@hiddenhippo](https://www.ruby-forum.com/u/hiddenhippo)\
**Post date:** [December 28, 2007, 5:41pm UTC](https://www.ruby-forum.com/t/belongs-to-has-many-listing/124438/1 "2007-12-28T17:41:41Z")

</div>

Wasn’t sure what to call the subject, but here’s my issue.

I’ve got two models, and for arguments sake they’re called model1 and  
model2. model1 has many models2 and therefore I use the following  
within the ‘model’ files

model1  
has\_many :models2

model2  
belongs\_to :model1 …etc

However on my HTML listing page I want to show all the model1’s and  
their model2 associates. For example,

model1 (foo1)  
model2 (bar1)  
model2 (bar2)  
model2 (bar3)  
model1 (foo2)  
model2 (bar4)  
model2 (bar5)  
model2 (bar6)  
model2 (bar7)

So far I’ve only ever displayed one model and then used association to  
get it’s children, for example

@model1 = Model1.find(params[:id])  
@model2 = @model1.model2.find(:all)

The above works nicely, however for my new project I do;  
@model1 = Model1.find(:all)  
@model2 = Model2.find(:all)

I loop over @model1 and then have to loop on @model2 to see whether  
any of them belong to the @model1 I’m currently reading. Is their a  
nicer way to ‘filter’ @model2 into a new variable containing only the  
items relating to the current @model1? I just want my code to be  
cleaner and more ‘rubyfied’

Thanks in advance

---

<div class="post-metadata">

**Author:** ![hiddenhippo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/65b543/32.png) [@hiddenhippo](https://www.ruby-forum.com/u/hiddenhippo)\
**Post date:** [December 28, 2007, 10:34pm UTC](https://www.ruby-forum.com/t/belongs-to-has-many-listing/124438/2 "2007-12-28T22:34:59Z")

</div>

On Dec 28, 8:40 am, hiddenhippo [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> model2 (bar5)
> model2 (bar6)
> model2 (bar7)
> 
> ```

Is @model1.model2s what you’re looking for?

///ark

---

<div class="post-metadata">

**Author:** ![hiddenhippo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/65b543/32.png) [@hiddenhippo](https://www.ruby-forum.com/u/hiddenhippo)\
**Post date:** [December 29, 2007, 2:01pm UTC](https://www.ruby-forum.com/t/belongs-to-has-many-listing/124438/3 "2007-12-29T14:01:05Z")

</div>

I don’t believe so because model1 is an array of database values, and  
the relationship to model2 is a one to many. At the moment I i’ve  
got

@model1s = Model1.find(:all)  
@model2s = Model2.find(:all)

When I render out each of the entries in @model1s I have to loop over  
@model2s in order to find the related values and render those.  
Normally if I’d only got one value, i.e. not an array, within @model1  
then I would do

@model1 = Model1.find(1)  
@model2 = @model1.model2.find(:all)

To bottom line it I was just hoping these was a nice rubyish/rails way  
of handling this.

Thanks for you reply and hopefully the above makes things a little  
clearer.

---

<div class="post-metadata">

**Author:** ![hiddenhippo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/65b543/32.png) [@hiddenhippo](https://www.ruby-forum.com/u/hiddenhippo)\
**Post date:** [December 29, 2007, 8:50pm UTC](https://www.ruby-forum.com/t/belongs-to-has-many-listing/124438/4 "2007-12-29T20:50:43Z")

</div>

On Dec 29, 5:00 am, hiddenhippo [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I don’t believe so because model1 is an array of database values

Actually, in my code @model1 was one of those values. @model1.model2s  
then gives you all the Model2 records associated with that particular  
Model1 record.

> When I render out each of the entries in @model1s I have to loop over  
> @model2s in order to find the related values and render those.

No need to loop.

///ark

---

<div class="post-metadata">

**Author:** ![hiddenhippo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/65b543/32.png) [@hiddenhippo](https://www.ruby-forum.com/u/hiddenhippo)\
**Post date:** [December 29, 2007, 3:15pm UTC](https://www.ruby-forum.com/t/belongs-to-has-many-listing/124438/5 "2007-12-29T15:15:23Z")

</div>

On Dec 28, 2007 11:40 AM, hiddenhippo [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> model2  
> model2 (bar4)  
> The above works nicely, however for my new project I do;  
> @model1 = Model1.find(:all)  
> @model2 = Model2.find(:all)
> 
> I loop over @model1 and then have to loop on @model2 to see whether  
> any of them belong to the @model1 I’m currently reading. Is their a  
> nicer way to ‘filter’ @model2 into a new variable containing only the  
> items relating to the current @model1? I just want my code to be  
> cleaner and more ‘rubyfied’

I’d do this with partials.

class Model1Controller

def index  
@models = Model1.find(:all)  
end  
end

views/model1/index.rhtml (or .html.erb if you are using Rails 2.0)

```
<%= render :partial => 'model1', :collection => @models %>

```

views/model1/\_model1.rhtml

```
 <% # whatever you need to render the model1 stuff itself %>
 <%= render :partial => 'model2', :collection => model1.model2s %>

```

views/model1/\_model2.rhtml

```
  <% # whatever you need to render a model2 %>

```

–  
Rick DeNatale

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