Database design 我可以用多个具体的超级表来实现这一点吗?
我有两个表格,“目录”和“档案”,可以评论和评级。我已经考虑过使用多态关联,并决定不使用它。如果我使用多态关联,表“ratings”和“comments”都会有这个特性 通过具体的supertable实现,这可能吗?如果是这样,我该怎么做?试试这个:Database design 我可以用多个具体的超级表来实现这一点吗?,database-design,polymorphic-associations,Database Design,Polymorphic Associations,我有两个表格,“目录”和“档案”,可以评论和评级。我已经考虑过使用多态关联,并决定不使用它。如果我使用多态关联,表“ratings”和“comments”都会有这个特性 通过具体的supertable实现,这可能吗?如果是这样,我该怎么做?试试这个: class Commentable < ActiveRecord::Base has_many :comments has_one :profile, :content end class Comment < ActiveR
class Commentable < ActiveRecord::Base
has_many :comments
has_one :profile, :content
end
class Comment < ActiveRecord::Base
belongs_to :commentable
end
class Content < ActiveRecord::Base
belongs_to :commentable
end
class Profile < ActiveRecord::Base
belongs_to :commentable
end
要从配置文件中检索数据库中的注释,请使用以下技巧:
# Profile.rb
# instead of doing a has many through,
# we can just use instance method with custom join.
def comments
find(self.id, :joins => "JOIN commentables cm ON (profiles.id = cm.id)
LEFT OUTER JOIN comments c ON (cm.id = c.commentable_id)")
end
未经测试的代码
有关更多详细信息和解释,请参见此处,您在连接上混淆了列。应该是这样的:
find(self.id, :joins => "JOIN commentables cm ON (profiles.commentable_id = cm.id)
LEFT OUTER JOIN comments c ON (cm.id = c.commentable_id)")
如果我错了,请纠正我,但您不能在Profiles.rb中执行此操作吗
def comments
Comments.find(:all, :conditions => ["commentable_id = ?", self.commentable_id])
end
def comments
Comments.find(:all, :conditions => ["commentable_id = ?", self.commentable_id])
end