如何解决Rails 5中的错误“ HasManyThroughCantAssociateThroughHasOneOrManyReflection”?

时间:2019-01-12 19:02:35

标签: ruby-on-rails ruby ruby-on-rails-5

我已经创建了简单的Rails应用,当我要发布图像时,在Rails管理员中显示了错误“ HasManyThroughCantAssociateThroughHasOneOrManyReflection”。

这是我的代码:

有人可以帮我解决吗?

class ProductInvest < ApplicationRecord
  belongs_to :product
  has_many :pictures, through: :product
end

class Product < ApplicationRecord
  has_many :pictures
  has_many :Product_invests
end

class Picture < ApplicationRecord
  belongs_to :product
  has_many :Product_invests, through: :product
end

1 个答案:

答案 0 :(得分:0)

您的关联设置不正确。尝试以下方法:

class ProductInvest < ApplicationRecord
  has_many :products
  has_many :pictures, through: :products
end

class Product < ApplicationRecord
  belongs_to :pictures
  belongs_to :product_invests
end

class Picture < ApplicationRecord
  has_many :products
  has_many :product_invests, through: :products
end

您可以在此处获取更多信息:RubyGuides