Ruby on rails has_one至未提供“创建_子对象”方法吗?

Ruby on rails has_one至未提供“创建_子对象”方法吗?,ruby-on-rails,activerecord,ruby-on-rails-3.1,associations,polymorphic-associations,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 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'
class用户未定义的方法“create#u image”

没错。您需要首先构建/创建图像分配

u.create_image_assignment
u.image_assignment.create_image

啊,好吧,这就是我最后要做的。非常感谢。