# MTM relationship with relationship attributes

**URL:** <https://www.ruby-forum.com/t/mtm-relationship-with-relationship-attributes/87738>\
**Category:** Rails\
**Created:** [March 5, 2007, 6:00pm UTC](https://www.ruby-forum.com/t/mtm-relationship-with-relationship-attributes/87738 "2007-03-05T18:00:15Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![vishu.raj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/f4b2a3/32.png) [@vishu.raj](https://www.ruby-forum.com/u/vishu.raj)\
**Post date:** [March 5, 2007, 6:00pm UTC](https://www.ruby-forum.com/t/mtm-relationship-with-relationship-attributes/87738/1 "2007-03-05T18:00:15Z")

</div>

I have following many to many relationship:  
class Service \< ActiveRecord::Base  
has\_many :service\_dependencies  
has\_many :dependents, :through =\> :service\_dependencies  
end

class ServiceDependency \< ActiveRecord::Base  
belongs\_to :service  
belongs\_to :dependent, :class\_name =\> ‘Service’, :foreign\_key =\>  
‘dependent\_service\_id’  
end

However, in my schema the MTM table service\_dependencies has few  
relationship attributes as well, e.g. impact, severity, etc. along  
with the foreign keys.

Right now, I’m displaying the dependent services’ fields in following  
manner:  
\<%=h dependents.send(“service\_name”) %\>

\<% for dependents in @service.dependents %\>  
**Service Name:**  
 \<%=h dependents.send(“service\_name”)  
%\>  
  
\<% end %\>

I also want to display the relationship attributes, could someone  
please help me by letting me know how to access them in my view page?

---

<div class="post-metadata">

**Author:** ![vishu.raj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/f4b2a3/32.png) [@vishu.raj](https://www.ruby-forum.com/u/vishu.raj)\
**Post date:** [March 6, 2007, 1:32pm UTC](https://www.ruby-forum.com/t/mtm-relationship-with-relationship-attributes/87738/2 "2007-03-06T13:32:19Z")

</div>

Hi,

I found out the solution myself by refering to several examples and  
forums. It is as easy as this:

\<% for dependent in @service.dependents %\>  
**Service Name:**  
 \<%=h dependent.send(“service\_name”)  
%\>  
  
\<% dependency = @service.service\_dependencies.find(:first,  
:conditions =\>  
[“dependent\_service\_id = ?”, dependent.id])%\>  
**Impact:**  
 \<%=h dependency.send(“impact”) %\>

\<%end%\>

This may be helpful for some other beginner like me.

Thanks and Regards,  
Vishu

---

<div class="post-metadata">

**Author:** ![vishu.raj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/f4b2a3/32.png) [@vishu.raj](https://www.ruby-forum.com/u/vishu.raj)\
**Post date:** [March 6, 2007, 2:16pm UTC](https://www.ruby-forum.com/t/mtm-relationship-with-relationship-attributes/87738/3 "2007-03-06T14:16:57Z")

</div>

Vishwaraj wrote:

> ```
> ["dependent_service_id = ?", dependent.id])%>
> <b>Impact:</b><br> <%=h dependency.send("impact") %><br>
> 
> ```
> 
> \<%end%\>
> 
> This may be helpful for some other beginner like me.

Better would be

\<% sds = @service.service\_dependents.find(:all, :include =\> :dependent)  
for service\_dependent in sds %\>  
**Service Name:**  
  
\<%= h service\_dependent.dependent.service\_name %\>  
  
**Impact:**  
  
\<%= h service\_dependent.impact %\>  
  
\<%end%\>

–  
We develop, watch us RoR, in numbers too big to ignore.
