Topic: Can I use has_many with polymorphic childrens?
I am developing a website with diverse content, each content type is a separate model using a separate database.
However, each type of content belongs to a specific catalog. Catalogues can be nested:
class Catalog < ActiveRecord::Base
has_many :children, :dependent => :destroy, :source => :images # but different types of content use different DB tables :(
belongs_to :parent, :class_name => 'Catalog', :foreign_key => "parent_id"
# table fields: parent_id:int, name:string, description:text ...
end
# content types:
class Image < ActiveRecord::Base
belongs_to :parent, :class_name => 'Catalog', :foreign_key => "parent_id"
# table fields: parent_id:int, name:string, filepath:string, ...
end
class Product < ActiveRecord::Base
belongs_to :parent, :class_name => 'Catalog', :foreign_key => "parent_id"
# table fields: parent_id:int, name:string, cost:float, ...
end
# other content types ....Maybe my model design is wrong, but I don`t know right solution
Last edited by somewater (2012-04-04 07:11:07)