Topic: Getting at parameters of join table in :through relationship

I have am new here.

I am trying to do something along the following lines:

class Person < ActiveRecord::Base
  has_many :affiliations
  has_many :organizations, :through => :affiliations

class Organization < ActiveRecord::Base
  has_many  :affiliations
  has_many  :people, :through => :affiliations
  has_many  :board_members, :through => :affiliations, :source => :person,
            :conditions => 'affiliations.relationship = "Board Member"'         

In my "join table" (affiliations), there are columns beyond just "person_id" and "organization_id" -- for example, we may have a "remarks" column of type mediumtext.  My question is, how can I get at these other columns from my view?  Here is an example:

Now, in the view, I have a page for showing an organization, and in that page, I hae:
<!-- other content before here -->
<% for board_member in @organization.board_members %>
  <li><%= link_to(board_member.first+" "+board_member.last,
                    :controller => 'person',
                    :action => 'show',
                    :id => board_member) %><br />
    XXX Here is where I want to put affiliation.remarks XXX
<!-- other content after here -->

Any suggestions?  I've tried board_member.affiliation.remarks with no success...

Re: Getting at parameters of join table in :through relationship

I'm fairly confident that the :through relationship does not create the accessor functions in your joined table for you, but it certainly does not stop you from creating a model for affilications:

Class Affiliation < ActiveRecord
  has_many :organizations
  has_many :people

  #those relationships should create all the methods you'd want
  #to access things via an affiliation instance,
  #ie affiliation.remarks or affiliation.person

It's inelegant, but it works.

Last edited by circuitshaman (2006-11-20 16:24:00)

If art interprets our dreams, the computer executes them in the guise of programs.

-SICP (Abelson, Sussman)

Re: Getting at parameters of join table in :through relationship

I am still a novice here, but I do not think that solution will work.  Currently I have the following model for "affiliations" (my join table):

class Affiliation < ActiveRecord::Base
  belongs_to :organization
  belongs_to :person

Each affiliation has one person and one organization (along with some other attributes in the affiliations table that I'd like to access).  If I change the model from "belongs_to" to "has_one", I get the following error:

Invalid source reflection macro :has_one for has_many :organizations, :through => :affiliations.  Use :source to specify the source reflection.

Just again as a refresher, what I am trying to do here is the following:

I have People and Organizations.  They are joined through a table of Affiliations.  The affiliations table has person_id and organization_id, but it also has other columns. 

So, for example, in organization.rb, you might see:
  has_many  :people, :through => :affiliations

In the view for organization, I have code to the following:

Affiliated People:
<% for person in @organization.people %>
    <li><%= person.first+" "+person.last -%> (here's where I'd like to show affiliation.remarks)</li>
<% end %>

But what I'd really like to also include here are some attributes from the record that joined them in the affiliations table.  How do I do this?  Note that people can have many affiliations, and organizations can have many affiliations.  Furthermore, people and organizations can have multiple affiliations between each other, for example, a person could be both affiliated as an Employee and as a Board Member.

Thanks in advance for any/all info on how to proceed here. 

Re: Getting at parameters of join table in :through relationship

I'm subscribed because I was interested to see if there was a better approach than:

<% @organization.affiliations.each do |affiliation| %>
    <li><%= affiliation.person.first+" "+affiliation.person.last + affiliation.remarks -%></li>
<% end %>


Re: Getting at parameters of join table in :through relationship

That solution does not work, to the best of my knowledge.  Allow me to simplify what I am asking.  Take a look at this page, which provides a simple explanation of different data models: … ps-diagram

Note the last relationship, described as "Student has many clubs through memberships."  In the join table (memberships), there is a "joined_on" field.

Explain to me how the code would look in the view if you wanted to show a list of all the clubs to which a student belonged, and the date on which they joined each club.

This is the simple question I am trying to ask -- how do you get at fields in the join table specific to each individual join?

Re: Getting at parameters of join table in :through relationship

Samsm's solution should work. The trick is you need to loop through the memberships, not the clubs. For example:

<% for membership in @student.memberships %>
  Club: <%= %>
  Joined on: <%= membership.created_at %>
<% end %>

Railscasts - Free Ruby on Rails Screencasts