MTM relationship with relationship attributes


I have following many to many relationship:
class Service < ActiveRecord::Base
has_many :service_dependencies
has_many :dependents, :through => :service_dependencies

class ServiceDependency < ActiveRecord::Base
belongs_to :service
belongs_to :dependent, :class_name => ‘Service’, :foreign_key =>

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
<%=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?



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 = ?”,])%>
<%=h dependency.send(“impact”) %>


This may be helpful for some other beginner like me.

Thanks and Regards,


Vishwaraj wrote:

        ["dependent_service_id = ?",])%>
  <b>Impact:</b><br> <%=h dependency.send("impact") %><br>


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 %>


<%= h service_dependent.impact %>


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