【问题标题】:Migrating from active record/postgres to mongodb/mongoid and I cannot create/save从活动记录/postgres 迁移到 mongodb/mongoid,我无法创建/保存
【发布时间】: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>

标签: ruby-on-rails ruby mongodb mongoid


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-10-06
    • 2013-05-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-14
    • 2020-05-22
    相关资源
    最近更新 更多