我的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"
答案 0 :(得分:2)
只发贴给我删除的其他人
as: :imageable
现在正常工作