gpt4 book ai didi

ruby-on-rails - Mongoid has_many/belongs_to 有 'undefined method' 错误

转载 作者:可可西里 更新时间:2023-11-01 10:37:38 24 4
gpt4 key购买 nike

我想在我的 mongoid 模型中使用 has_many/belongs_to。

我已经在其他模型中使用它并且效果很好,但在下面的代码中会出现

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

Controller :

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

如何修复错误?

最佳答案

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

关于ruby-on-rails - Mongoid has_many/belongs_to 有 'undefined method' 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53985117/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com