I have a custom model which uses attachment model in rails. My attachment model looks something like this
class Attachment < ActiveRecord::Base
belongs_to :attachable, polymorphic: true
has_attached_file :file, styles: { logo: ['200x50>',:png] }
end
and the other model that uses the attachment looks something like this
class User < ActiveRecord::Base
has_many :attachments, as: :attachable, dependent: :destroy
end
I want user model to have another attachment different from the one I have already setup to upload logo, something like this
has_one :user_logo, -> {where attachable_type: "UserLogo"}, class_name: "Attachment", foreign_key: :attachable_id, foreign_type: :attachable_type, dependent: :destroy
but when I try to access attachment.attachable
I get
undefined UserLogo as **UserLogo** is not a model
.
Can anyone please suggest what changes can I make so that attachment.attachable
works for both attachment type.
so for example
when i execute something like this
att = Attachment.find(3)
#assume it returns attachable type as User
so att.attachable returns user object
but when i execute
att = Attachment.find(3)
#assume it returns attachable type as UserLogo
so att.attachable returns exception wrong constant name UserLogo
how can i access User
object from both attachment types. Thanks