Hello Suppose there is the following architecture: class Relation < ActiveRecord::Base belongs_to :owner, :class_name => ''Unit'', :foreign_key => :owner_id belongs_to :property, :class_name => ''Unit'', :foreign_key => :property_id end class Unit < ActiveRecord::Base belongs_to :instance, :polymorphic => true has_many :relations_to, :class_name => ''Relation'', :foreign_key => :owner_id has_many :relations_from, :class_name => ''Relation'', :foreign_key => :property_id has_many :properties, :through => :relations_to has_many :owners, :through => :relations_from end class Task < ActiveRecord::Base has_one :unit, :as => :instance end class Message < ActiveRecord::Base has_one :unit, :as => :instance end So the basic idea is that there are units that can have multiply owners and properties among other units, so we have many-to-many association of the units table with itself. As you can see task and message have polymorphic associations with unit, they are units. I wonder if there is an easy way to find all related tasks/messages to some unit (as association proxies), for example find all tasks as properties of the unit, like: unit.properties.tasks I want to do things like: new_property_message = Message.new unit.properties.messages << new_property_message new_owner_task = Task.new unit.owners.tasks << new_owner_task unit.owners.messages.find :all, :conditions => { :title => ''hello'' } Thanks --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Ivan Ukhov wrote:> > I want to do things like: > > new_property_message = Message.new > unit.properties.messages << new_property_message > > new_owner_task = Task.new > unit.owners.tasks << new_owner_task > > unit.owners.messages.find :all, :conditions => { :title => ''hello'' } > > ThanksSo what happened when you tried it? Irregardless of polymorphic associations, you can''t associate through the << operator (as you did above) if one (or both) of the records are new. ilan -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Ilan Berci wrote:> Ivan Ukhov wrote: > >> >> I want to do things like: >> >> new_property_message = Message.new >> unit.properties.messages << new_property_message >> >> new_owner_task = Task.new >> unit.owners.tasks << new_owner_task >> >> unit.owners.messages.find :all, :conditions => { :title => ''hello'' } >> >> Thanks > > So what happened when you tried it? > > Irregardless of polymorphic associations, you can''t associate through > the << operator (as you did above) if one (or both) of the records are > new. > > ilanWhoops.. I meant you can''t associate through the << operator in a MANY TO MANY case if one or both records are new ilan -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
There are no such methods union.[owners | properties].[messages | tasks]... i don''t mean only << method (if there''s no such, let it be), i mean all the functionality that the association proxy gives (scope reading/writing etc) On Feb 29, 6:30 am, Ilan Berci <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Ivan Ukhov wrote: > > > I want to do things like: > > > new_property_message = Message.new > > unit.properties.messages << new_property_message > > > new_owner_task = Task.new > > unit.owners.tasks << new_owner_task > > > unit.owners.messages.find :all, :conditions => { :title => ''hello'' } > > > Thanks > > So what happened when you tried it? > > Irregardless of polymorphic associations, you can''t associate through > the << operator (as you did above) if one (or both) of the records are > new. > > ilan > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---