所以我设置了一些模型,每个模型都可以发表评论。我已经使用 has_many_polymorphs 设置了它,但是我开始遇到一些问题,它无法按照我认为的方式工作。
例如:
class Project < ActiveRecord::Base
end
class Message < ActiveRecord::Base
has_many_polymorphs :consumers,
:from => [:projects, :messages],
:through => :message_consumers,
:as => :comment # Self-referential associations have to rename the non-polymorphic key
end
class MessageConsumer < ActiveRecord::Base
# Self-referential associations have to rename the non-polymorphic key
belongs_to :comment, :foreign_key => 'comment_id', :class_name => 'Message'
belongs_to :consumer, :polymorphic => true
end
在这种情况下,删除项目时消息不会被删除,因为消息实际上是关系中的父级。
我对示例进行了一些简化,但还有其他模型具有消息,并且也有类似工作的附件。
什么是正确的设置方法,以便在删除父级时删除子级?我希望没有一百万张桌子,但我想不出另一种方法来做到这一点。