【发布时间】:2020-07-14 07:01:50
【问题描述】:
我在 mongoid 中有以下模型(即,以前处于活动记录中)。尝试保存产品项目组以及项目选择选项,但我没有遇到方法“all_of”错误。而且我是使用 mongoid/ 的新手带有 ROR 的 mongobd。
class ProductItemGroup
include Mongoid::Document
include Mongoid::Timestamps
store_in database: "cnc_product_ms"
field :name, localize: true
field :description, localize: true
field :product_item_selection_type, type: String
embeds_one :item_selection_option
has_and_belongs_to_many :service_categories
field :service_category_ids, type: Array
end
class ItemSelectionOption
include Mongoid::Document
include Mongoid::Timestamps
include SimpleEnum::Mongoid
store_in database: "cnc_product_ms"
as_enum :selection_type,multiple: 1, single: 2
as_enum :mandatory_selection_type, atleast_one: 1, many: 2
field :selection_type, type: Integer
field :selection_mandatory, type: Boolean, default: false
field :mandatory_selection_type, type: Integer
embedded_in :product_item_group
end
controller /form
class ProductItemGroupForm < ApplicationForm
attr_accessor(:product_item_group)
def create
@product_item_group = ProductItemGroup.new(params)
@product_item_group.save!
end
end
保存!,我遇到以下错误
NoMethodError: undefined method `all_of' for #<Class:0x00007fd96afea600>
from /Users/90srebel/.rvm/gems/ruby-2.6.4/gems/activerecord-6.0.3.1/lib/active_record/dynamic_matchers.rb:22:in `method_missing'
【问题讨论】:
-
我认为这不会解决您的问题,但将
service_category_ids定义为与has_and_belongs_to_many冲突的字段。一旦定义了has_and_belongs_to_many :service_categories,就会隐式创建字段service_category_ids。 -
我在没有声明 service_category_ids 的情况下进行了尝试,但我得到了错误 no fields found.So 声明了该字段。
-
NoMethodError (undefined method `fields' for #<0x00007ff58cc23830>0x00007ff58cc23830>
标签: ruby-on-rails ruby mongodb mongoid