首页 > 解决方案 > 从活动记录/postgres 迁移到 mongodb/mongoid,我无法创建/保存

问题描述

我在 mongoid 中有以下模型(即,以前在活动记录中)。尝试保存产品项目组以及项目选择选项,但我没有遇到任何方法“all_of”错误。而且我是使用 mongoid/mongobd 和 ROR 的新手.

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'

标签: ruby-on-railsrubymongodbmongoid

解决方案


得到了问题。我正在结束这个问题。我做错的是,我在 product_item_group 模型中声明了 has_and_belongs_to_many 但忘记在 service_category 模型中声明相同的东西。愚蠢的


推荐阅读