ruby-on-rails - has_one 是否没有提供 `create_child` 方法?

标签 ruby-on-rails activerecord ruby-on-rails-3.1 associations polymorphic-associations

我有以下型号:

class User < ActiveRecord::Base
  has_one :image_assignment, as: :imageable
  has_one :image, through: :image_assignment
end

class ImageAssignment < ActiveRecord::Base
  belongs_to :image
  belongs_to :imageable, polymorphic: true
end

class Image < ActiveRecord::Base
  mount_uploader :image, ImageUploader
end

u = User.new
u.create_image #=> undefined method `create_image'

最佳答案

这是正确的。您需要首先构建/创建 image_assignment

u.create_image_assignment
u.image_assignment.create_image

关于ruby-on-rails - has_one 是否没有提供 `create_child` 方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10587306/

相关文章:

sql - 当某些东西存储为数组时的 Rails where 子句

ruby-on-rails - 如果缺少翻译,如何使用默认语言环境

ruby-on-rails - Rails 5 仅当属性当前为 nil 时才更新属性

ruby-on-rails - rails 3.1 : Sass Import from Lib

ruby-on-rails - 清理 View ruby​​ 逻辑并将关注点分离到模型/ Controller 中

ruby-on-rails - 为 Controller 切换 Assets 主机

configuration - ActionMailer 设置在开发和生产之间存在错误差异

mysql - rails 3 : Why is flatten duplicating records with more than one associated record?

ruby-on-rails - 如何为rails中的单个请求跨模型设置变量?

ruby-on-rails - ActiveRecord 列表自定义范围