# Including join model data

**URL:** <https://www.ruby-forum.com/t/including-join-model-data/156888>\
**Category:** Rails\
**Created:** [January 4, 2009, 10:50pm UTC](https://www.ruby-forum.com/t/including-join-model-data/156888 "2009-01-04T22:50:16Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![tlianza](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ce7236/32.png) [@tlianza](https://www.ruby-forum.com/u/tlianza)\
**Post date:** [January 4, 2009, 10:50pm UTC](https://www.ruby-forum.com/t/including-join-model-data/156888/1 "2009-01-04T22:50:16Z")

</div>

I`m using the joing model Part to access Treinando from Evento.  
This code works well:

@treinandos = Evento.find(params[:evento\_id]).treinandos

But the parts table has some more columns I need to retrieve  
addtionally.  
Even after some docs/googling and tests I was not able to append this  
information to the resulting object @treinandos.

Can you give some hints about how to do it?

Models:

class Treinando \< ActiveRecord::Base  
has\_many :parts  
has\_many :eventos, :through =\> :parts  
end  
class Part \< ActiveRecord::Base  
belongs\_to :treinando  
belongs\_to :evento  
end  
class Evento \< ActiveRecord::Base  
has\_many :parts  
has\_many :treinandos, :through =\> :parts  
end

---

<div class="post-metadata">

**Author:** ![tlianza](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ce7236/32.png) [@tlianza](https://www.ruby-forum.com/u/tlianza)\
**Post date:** [January 5, 2009, 12:59am UTC](https://www.ruby-forum.com/t/including-join-model-data/156888/2 "2009-01-05T00:59:07Z")

</div>

Well, I found a way, but it is not a optimized (rails way) one, since  
it executes a find for each “part” record, more some selects.

```
parts = Evento.find(params[:evento_id]).parts
@treinandos = []
parts.each do |part|
  obj = Treinando.find(part.treinando_id)
  obj[:padrinho] = part.padrinho.nome if part.padrinho_id
  obj[:presenca] = part.presenca
  @treinandos << obj
end

```

If you have any idea about the rails way to do it, please tell me.
