# Finding a models asscoations

**URL:** <https://www.ruby-forum.com/t/finding-a-models-asscoations/112017>\
**Category:** Rails\
**Created:** [August 14, 2007, 10:51am UTC](https://www.ruby-forum.com/t/finding-a-models-asscoations/112017 "2007-08-14T10:51:40Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![johnnybutler7](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8491ac/32.png) [@johnnybutler7](https://www.ruby-forum.com/u/johnnybutler7)\
**Post date:** [August 14, 2007, 10:51am UTC](https://www.ruby-forum.com/t/finding-a-models-asscoations/112017/1 "2007-08-14T10:51:40Z")

</div>

Hi,

I cant seem to find the answer to this anywhere so any help/advice is  
appreciated.

Each project has one risk and a risk has a number of has/one belongs to  
assocations. So what i want to do is in my code loop through all the  
associations for a risk and display some field information from each as  
they are pretty similar. So somehting like

\<% for model\_association in project.risks %\>  
\<%= model\_association.model\_name %\>  
\<%= model\_or\_assocation.name %\>  
\<% end %\>

Each association to risk has a filed called name present which i would  
like displayed

Is this possible or whats the best way to something like this?

Johnny B

---

<div class="post-metadata">

**Author:** ![johnnybutler7](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8491ac/32.png) [@johnnybutler7](https://www.ruby-forum.com/u/johnnybutler7)\
**Post date:** [August 14, 2007, 12:31pm UTC](https://www.ruby-forum.com/t/finding-a-models-asscoations/112017/2 "2007-08-14T12:31:41Z")

</div>

Ihad to di a bit deeper but found the following in the API

[http://rails.rubyonrails.com/classes/ActiveRecord/Reflection/ClassMethods.html](http://rails.rubyonrails.com/classes/ActiveRecord/Reflection/ClassMethods.html)  
[http://rails.rubyonrails.com/classes/ActiveRecord/Reflection/MacroReflection.html](http://rails.rubyonrails.com/classes/ActiveRecord/Reflection/MacroReflection.html)

reflect\_on\_all\_associations looks like it will do the job

John B

John B. wrote:

> Hi,
> 
> I cant seem to find the answer to this anywhere so any help/advice is  
> appreciated.
> 
> Each project has one risk and a risk has a number of has/one belongs to  
> assocations. So what i want to do is in my code loop through all the  
> associations for a risk and display some field information from each as  
> they are pretty similar. So somehting like
> 
> \<% for model\_association in project.risks %\>  
> \<%= model\_association.model\_name %\>  
> \<%= model\_or\_assocation.name %\>  
> \<% end %\>
> 
> Each association to risk has a filed called name present which i would  
> like displayed
> 
> Is this possible or whats the best way to something like this?
> 
> Johnny B

---

<div class="post-metadata">

**Author:** ![johnnybutler7](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8491ac/32.png) [@johnnybutler7](https://www.ruby-forum.com/u/johnnybutler7)\
**Post date:** [August 14, 2007, 1:07pm UTC](https://www.ruby-forum.com/t/finding-a-models-asscoations/112017/3 "2007-08-14T13:07:28Z")

</div>

How can i actually i get the value of the object relationship, so the  
code below will output the class name of each association for the risk  
which is a start. So what i want is to display a field value belonging  
to the association

\<% for association in @project.risk.class.reflect\_on\_all\_associations %\>  
\<%= association.name %\>#This will print out the name

```
#So i need to get the association model name where the qoutes are 

```

below  
\<%= @project.material\_risk.“association.name”.field\_value %\>  
\<% end %\>

Can anyone help?

Johnny B
