Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Mongoid有很多/属于has';未定义的方法';错误_Ruby On Rails_Ruby_Mongodb_Mongoid - Fatal编程技术网

Ruby on rails Mongoid有很多/属于has';未定义的方法';错误

Ruby on rails Mongoid有很多/属于has';未定义的方法';错误,ruby-on-rails,ruby,mongodb,mongoid,Ruby On Rails,Ruby,Mongodb,Mongoid,我想在我的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

我想在我的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
class ProductsController

如何修复错误?

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

如果您可以提供
class Opt self.collection\u name='options'
而在产品使用中
有很多:opts
@RajarshiDas谢谢,但注意事项有所改变,选项是保留字,请将其更改为其他选项word@G.B谢谢,这很有效!很高兴知道它起作用了。那么也请接受答案