On Fri, Oct 29, 2004 at 02:42:45PM +0100, Simon Hall
wrote:> Does anyone know how to get a handle on an attribute on an associative
> table. Eg. Subject has_and_belongs_to_many Units, but some units are
> optional depending on the Subject.
>
> So have associative table called: subjects_units with the 2 FKs and an
> attribute is_optional. But this attribute does not propagate through
> ActiveRecord. I guess you could have a separate association Subject
> has_many Subject_Units (say) and pull it in that way, but this seems a
> little messy.
>
> Is there a neat way to extend the association to include this attribute?
>
> Many thanks,
> Simon (''Ronaldo'' on IRC)
You have to give it some new finder SQL with the :finder_sql option for
has_and_belongs_to_many. You can pull in extra fields from the join
table there.
e.g.
has_and_belongs_to_many :units, :finder_sql "..."
Then AR will use it''s magic and give you subject.units.is_optional .
-Scott