问题
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