Topic: Accessing attributes on join table?
Is there any way to access attributes on a join table used in has_many :through relationship?
An article has_many :readers through :readings.
A reader has_many :articles through :readings.
There are attributes on the readings join table (which is itself a model) such as rating and review:
If you are accessing a particular reader.article, is there any way to access the attributes of the reading record (such as rating and review)? E.g.:
for reader in Readers.find(:all)
reader.article.rating (this won't work, but it's the desired result)
That way, you can have a view spit out something like:
John Doe has read three articles:
Bla bla (4 stars)
Yada yada yada (2 stars)
Ok Ok Ok (1 star)
The only solution that has been suggested to me is to traverse the readings directly, which is sort of missing the entire point of the :through relationship. In that case, I basically have to rewrite the logic that rails is supposed to be doing for me with the :through concept, by iterating over all of the readings and screening out only that subset with the reader_id I care about.
I see discussion in some pages found through my googling that the has_and_belongs_to_many method has a deprecated feature that allows you to mess with attributes on the join table (push_with_attributes). I don't think what I am trying to is particularly novel. Even just getting the ID of the record in the join table that connected this article to this reader would be enough or me to subsequently get at the attributes of the join table that I care about.