We have a User model and a Connection model which basically represents edges in a digraph (User to User, self-referenctial). Each of these edges can have a :connection_type which is currently just a char(1).
has_many :friendships, :class_name => 'Connection', :foreign_key => "source_user_id", :conditions => "connection_type = 'f'" has_many :friends, :through => :friendships, :source => :target_user
Using has_many :through we're able to connect up reading through reflections, but the << operator fails since our database constrains the connection_type to be from a certain set of values. We get around this using an extension (also using bugfix #6446:
has_many :friendships, :class_name => 'Connection', :foreign_key => "source_user_id", :conditions => "connection_type = 'f'" has_many :friends, :through => :friendships, :source => :target_user do def construct_join_attributes(associate) super.merge({:connection_type => 'f'}) end end
This enables the << operator to work as expected. The problem is that :construct_join_attributes is a protected method and this just doesn't feel like something that should be overridden. It's only used from the << operator, but the name isn't very generic for how we're using it here.
Would there be any interest in extending has_many :through to take a :create option w/ a hash that should be merged in as I'm doing here? It would make the << and add operators work in this case, and maybe be useful in others.
--Ian