imageable type is not setting for polymorphic associations

荒凉一梦 提交于 2019-12-20 03:18:16

问题


I have two models in my rails 4.2 application

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

and

class Image < ActiveRecord::Base
    belongs_to :imageable, polymorphic: true
end

I have problem with imageable_type which is not getting set accordingly for example if I do this in console

landingpage = LandingPage.first
image = landingpage.build_section2_photo

but when I do

image.imageable_type

i get

"LandingPage"

rather than

"Section2Photo"

回答1:


just posting for others I removed

as: :imageable

and working fine now



来源:https://stackoverflow.com/questions/34328735/imageable-type-is-not-setting-for-polymorphic-associations

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!