I have same problem as : Rails Polymorphic Association with multiple associations on the same model
But the solutions on this question aren't working for me. I have a picture model and an event model. Event has many pictures and one cover pic. Here are both the models.
class Picture < ActiveRecord::Base
belongs_to :image, polymorphic: true
end
class Event < ActiveRecord::Base
has_many :pictures, as: :image, :dependent => :destroy
has_one :cover_picture, -> { where image_type: "CoverPicture"},
class_name: Picture, foreign_key: :image_id,
foreign_type: :image_type, dependent: :destroy
end
Issue here is that when I create a new picture and set it as cover_picture of an event, it doesn't set the image_type to "CoverPicture". When I try and save it after specifically setting the image_type to "CoverPicture", it errors out with "NameError: uninitialized constant CoverPicture"