has_one是否通过不提供`create_child`方法?

时间:2012-05-14 16:24:51

标签: 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'

1 个答案:

答案 0 :(得分:2)

这是正确的。你需要先建立/创建image_assignment

u.create_image_assignment
u.image_assignment.create_image