accessing join model attributes in has_many :through

Hi --

After scouring these forums, the blogs, & wiki's I've finally decided to ask the question because either nobody has encountered this problem or, more likely, I I'm missing something obvious.

I am attempting to access the extra attributes on the join model through a has_many :through association. Most code examples I have found don't deal with this situation even though, I think, that was part of decision to deprecate HABTM in favor of HMT.

I have tables "accounts", "links", and the join table "account_links".

In the end, I want to be able to do this:

a = Account.find(1) a.links[0].label

=> "untitled"

You're calling "label" on a Link object, but links don't have a label field or attribute. So somewhere along the way you have to get hold of the AccountLink object you want.

class Link < ActiveRecord::Base has_many :account_links has_many :account, :through => :account_links

That should be :accounts (plural), though I don't think it has a direct bearing on the question you're asking.

David

Hi --

You're calling "label" on a Link object, but links don't have a label field or attribute. So somewhere along the way you have to get hold of the AccountLink object you want.

I guess what I was hoping for was that by association it would tag on the extra attributes from the AccountLink object to the Link object. That way, I could loop through the links collection and grab both "label" and "url" at the same time.

There are a couple of problems with that. First, what if Link objects also had a label field? Second, you can in theory have more than one AccountLink between a given Account and a given Link. You can use the :uniq flag to make this not happen, but it's still kind of contrary to the design overall to have unknown methods on an Account object automatically delegated to an AccountLink object.

David