是否可以在多态模型中使用delegate
或has_many
关联has_one
?这是如何运作的?
class Generic < ActiveRecord::Base
...
belongs_to :generable, polymorphic: true
delegate :file_url, to: :image, allow_nil: true
delegate :type_cat, to: :cat, allow_nil: true
end
class Image < ActiveRecord::Base
...
has_one :generic, as: generable, dependent: :destroy
end
class Cat < ActiveRecord::Base
...
has_one :generic, as: generable, dependent: :destroy
end