polymorphic associations and association proxy

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

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

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)