簡體   English   中英

Mongoid has_many/belongs_to 有“未定義方法”錯誤

[英]Mongoid has_many / belongs_to has 'undefined method' error

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

控制器 :

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

我該如何修復錯誤?

您正在使用保留字的選項。 Option更改為其他一些詞,例如ProductOption

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM