Rails 3 - 将多态图像模型附加为每个模型的不同图像类型,我做错了什么?
我不断收到 ActiveRecord::UnknownAttributeError:
Unknown attribute: imageable_id
代码:
多态图像模型:
class Image < ActiveRecord::Base
mount_uploader :asset, ImageUploader
belongs_to :imageable, :polymorphic => true
end
尝试对 2 种不同图像类型进行多态关联的模型:
has_one :image, :as => :imageable, :dependent => :destroy
accepts_nested_attributes_for :image
has_one :thumbnail, :as => :imageable, :dependent => :destroy
accepts_nested_attributes_for :thumbnail
尝试构建图像的控制器操作(实际上第一个“build_image”火灾,错误引用“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
!
谢谢
i keep getting an ActiveRecord::UnknownAttributeError:
unknown attribute: imageable_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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你只需要添加一个 :class_name =>; has_one :thumbnail 关系的图像选项。 (谢谢你检查我@Tilo)
You just need to add a :class_name => Image option to the has_one :thumbnail relationship. (thx for checking me @Tilo)