Is it possible to have one class containing tw


#1

Hello fellow Rail-builders,

Is it possible to declare one class that includes two polymorphic
fields as such:

class Mark < ActiveRecord::Base

attribute: value, :string

belongs_to :markable, polymorphic => true
end

class Property < ActiveRecord::Base
has_one :name, :as => :markable
has_one :value, :as => :markable
end

?

Based on what I know of the underlying implementation, this looks
impossible. One alternative is to declare artificial classes
PropertyName and PropertyValue, each of which has_one :value, :as =>
markable and then change Property to has_one :property_name and has_one
:property_value:

class PropertyName < ActiveRecord::Base
has_one :value, :as => :markable
end

class PropertyValue < ActiveRecord::Base
has_one :value, :as => :markable
end

class Property < ActiveRecord::Base
has_one :property_name
has_one :property_value
end

, which isn’t that unnatural – however, just having additional classes
holding, only id references, seems like not as great as if the
polymorphic implementation in Mark class can actually allow Class#Field
as the value for markable_type.

What do you think? Any other suggestion? (BTW, yes, I suppose
Property can add :through assocation to directly access :markable for
convenience.)

Thanks!