Forum: Ruby on Rails Is it possible to have one class containing tw

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Philip (Guest)
on 2006-04-22 04:42
 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!
This topic is locked and can not be replied to.