Join table attributes piggy backed with has_many through


#1

Is there anyway to get the join table attributes to show up as
attributes on the has_many association when the has_many is eager
loaded. I think the eager loaded part is the gotcha to all this.

For example, if I have book.contributors, and the join table
“contributions” had a contribution_date on it. How can I get the date
onto the contributors’ objects.

That way I can do book.contributors[0].contribution_date. Is it not
possible to do when its included?


#2

Aryk G. wrote:

Is there anyway to get the join table attributes to show up as
attributes on the has_many association when the has_many is eager
loaded. I think the eager loaded part is the gotcha to all this.

For example, if I have book.contributors, and the join table
“contributions” had a contribution_date on it. How can I get the date
onto the contributors’ objects.

That way I can do book.contributors[0].contribution_date. Is it not
possible to do when its included?

The attributes should be there if you’re using has_and_belongs_to_many.

But if you’re using has_many_through, eager load like this

book = Book.find(…, :include => {:contributions => :contributor})

Then access like this:

book.contributions[0].contribution_date
book.contributions[0].contributor.name


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