我的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_类型有问题,例如,如果在控制台中执行此操作,则无法相应地设置该类型
landingpage = LandingPage.first
image = landingpage.build_section2_photo

但当我这么做的时候
image.imageable_type

我明白了
"LandingPage"

而不是
"Section2Photo"

最佳答案

只是为我删除的其他人发帖

as: :imageable

现在工作很好

10-08 03:56