Ian Duggan
2007-Jan-05 07:04 UTC
(Enhancement?) for self referential has_many :through, w/ scoped create...
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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Ian Duggan
2007-Jan-05 07:08 UTC
Re: (Enhancement?) for self referential has_many :through, w/ scoped create...
Ian Duggan wrote:> using an extension (also using bugfix #6446:Sorry, #6466. --Ian --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Reasonably Related Threads
- Self-Referential Many-To-Many relationships where the relationship itself has data.
- Self-referential has_many :through relationship
- Strange routing(?) Issue
- sshd can't access user files
- Any progress on undirected self-referential many to many relationships?