【问题标题】:Does has_one through not provide a `create_child` method?has_one through 是否不提供`create_child` 方法?
【发布时间】:2012-05-14 16:24:51
【问题描述】:

我有以下型号:

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'

【问题讨论】:

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


    【解决方案1】:

    没错。您需要先构建/创建 image_assignment

    u.create_image_assignment
    u.image_assignment.create_image
    

    【讨论】:

    • 啊,好吧,这就是我最终的结果。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-04
    • 1970-01-01
    • 2010-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多