【发布时间】:2015-12-17 07:16:02
【问题描述】:
我的 rails 4.2 应用程序中有两个模型
class LandingPage < ActiveRecord::Base
has_one :section2_photo, -> { where imageable_type: "Section2Photo"},
class_name: Image, foreign_key: :imageable_id, foreign_type: :imageable_type, dependent: :destroy, as: :imageable
has_one :section3_photo, -> { where imageable_type: "Section3Photo"},
class_name: Image, foreign_key: :imageable_id, foreign_type: :imageable_type, dependent: :destroy, as: :imageable
end
和
class Image < ActiveRecord::Base
belongs_to :imageable, polymorphic: true
end
我有 imageable_type 的问题,例如,如果我在控制台中这样做,它没有得到相应的设置
landingpage = LandingPage.first
image = landingpage.build_section2_photo
但是当我这样做时
image.imageable_type
我明白了
"LandingPage"
而不是
"Section2Photo"
【问题讨论】:
标签: postgresql ruby-on-rails-4 activerecord