Rails 3 - 将多态图像模型附加为每个模型的不同图像类型,我做错了什么?

发布于 2024-12-09 03:09:41 字数 838 浏览 8 评论 0原文

我不断收到 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

甜味超标? 2024-12-16 03:09:41

你只需要添加一个 :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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文