我想在我的Mongoid模型中使用has_many/归属。
我已经在其他模型中使用了它,它工作得很好,但是在下面的代码中

undefined method `options' for class `Product' error.

模型:
class Product
  include Mongoid::Document
  include Mongoid::Timestamps

  has_many :options

  field :name, type: String
  ...
end

class Option
  include Mongoid::Document
  include Mongoid::Timestamps

  belongs_to :product

  field :name, type: String
end

控制器:
class ProductsController < ApplicationController
  def index
    @products = Product.order(id: :desc)
  end
end

我怎样才能纠正这个错误?

最佳答案

您使用的选项是保留字。将Option改为其他单词,如ProductOption

10-07 12:25
查看更多