Belongs_to has_many listing

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

On Dec 28, 8:40 am, hiddenhippo [email protected] wrote:

model2 (bar5)
model2 (bar6)
model2 (bar7)

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

///ark

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.

On Dec 29, 5:00 am, hiddenhippo [email protected] 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

On Dec 28, 2007 11:40 AM, hiddenhippo [email protected] 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/

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs