可成像类型不是为多态关联设置的

时间:2015-12-17 07:16:02

标签: postgresql ruby-on-rails-4 activerecord

我的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"

1 个答案:

答案 0 :(得分:2)

只发贴给我删除的其他人

as: :imageable

现在正常工作