【问题标题】:imageable type is not setting for polymorphic associationsimageable 类型未设置为多态关联
【发布时间】: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


    【解决方案1】:

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

    as: :imageable
    

    现在工作正常

    【讨论】:

      猜你喜欢
      • 2019-03-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多