Ruby on rails Mongoid有很多/属于has';未定义的方法';错误
我想在我的mongoid模型中使用has_many/归属 我已经在其他模型中使用了它,它工作得很好,但是在下面的代码中出现了这种情况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
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谢谢,这很有效!很高兴知道它起作用了。那么也请接受答案