开发者

Rails 3 - Attaching polymorphic image model as different image types per each model, what am i doing wrong?

开发者 https://www.devze.com 2023-04-11 18:08 出处:网络
i keep getting an ActiveRecord::UnknownAttributeError: unknown attribute: image开发者_C百科able_id

i keep getting an ActiveRecord::UnknownAttributeError:

unknown attribute: image开发者_C百科able_id

the code:

polymorphic image model:

class Image < ActiveRecord::Base
  mount_uploader :asset, ImageUploader
  belongs_to :imageable, :polymorphic => true
end

the model that is attempting a polymorphic association for 2 different image types:

has_one :image, :as => :imageable, :dependent => :destroy 
accepts_nested_attributes_for :image

has_one :thumbnail, :as => :imageable, :dependent => :destroy 
accepts_nested_attributes_for :thumbnail

the controller action that attempts to build the images (the first "build_image" actually fires, the error references "build_thumbnail":

def new
  @item = @item_class.new #item is instantiated elsewhere
  @item.build_image #this works
  @item.build_thumbnail #this throws my error "unknown attribute: imageable_id"
end

thanks!

EDIT FIXED


You just need to add a :class_name => Image option to the has_one :thumbnail relationship. (thx for checking me @Tilo)

0

精彩评论

暂无评论...
验证码 换一张
取 消