1
有没有什么办法来定义source_type
里面的rails运行时间has_many
through
polymorphic
关联。如何定义运行时source_type里面“有很多通过”的多态关联?
下面的代码应该给你简要的想法..但它不工作..任何建议?
class A < ...
has_many :message_messagables, :foreign_key => :message_id
has_many :messagables, :through => :message_messagables, :source => :messagable, :source_type => lambda { |a| a.custom_type }
def custom_type
raise "needs to be defined inside subclass"
end
end
class MessageMessagable < ...
belongs_to :messagable, :polymorphic => true #[C, D]
belongs_to :message
end
class B < A
def custom_type
"C"
end
end
class E < A
def custom_type
"D"
end
end