Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/drupal/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 嵌入多个mongoid的原子addToSet_Ruby On Rails_Mongodb_Mongoid_Ruby On Rails 4_Mongoid3 - Fatal编程技术网

Ruby on rails 嵌入多个mongoid的原子addToSet

Ruby on rails 嵌入多个mongoid的原子addToSet,ruby-on-rails,mongodb,mongoid,ruby-on-rails-4,mongoid3,Ruby On Rails,Mongodb,Mongoid,Ruby On Rails 4,Mongoid3,我有以下操作: self.customers.where(.....).find_and_modify({ "$addToSet" => {:states => {"$each" => states }} }, new: true) 其中states是一组状态文档 该模型嵌入了许多:状态 不知怎的,它又回来了: 命名错误:未定义的方法“bson\u dump”用于 我理解错了什么吗?非常感谢您的帮助请记住,$each的值必须是一个可以序列化为BSON的Ruby数组。 如果使用

我有以下操作:

self.customers.where(.....).find_and_modify({ "$addToSet" => {:states => {"$each" => states }} }, new: true)
其中states是一组状态文档

该模型嵌入了许多:状态

不知怎的,它又回来了: 命名错误:未定义的方法“bson\u dump”用于


我理解错了什么吗?非常感谢您的帮助

请记住,$each的值必须是一个可以序列化为BSON的Ruby数组。 如果使用状态模型对象,则无法将它们的数组序列化为BSON。 要将它们与$each一起使用,请在每个状态模型对象上使用#serializable_hash,如下所示

测试/单元/客户测试.rb

require 'test_helper'
require 'json'
require 'pp'

class CustomerTest < ActiveSupport::TestCase
  def setup
    Customer.delete_all
  end

  test "find_and_modify with $addToSet and $each" do
    puts "\nMongoid::VERSION:#{Mongoid::VERSION}\nMoped::VERSION:#{Moped::VERSION}"
    customer = Customer.create('name' => 'John')
    customer.states << State.new('name' => 'New Jersey') << State.new('name' => 'New York')
    assert_equal 1, Customer.count

    states = [{'name' => 'Illinois'}, {'name' => 'Indiana'}]
    Customer.where('name' => 'John').find_and_modify({"$addToSet" => {:states => {"$each" => states}}}, new: true)
    assert_equal 4, Customer.first.states.size

    states = [State.new('name' => 'Washington'), State.new('name' => 'Oregon')]
    assert_raise NoMethodError do
      Customer.where('name' => 'John').find_and_modify({"$addToSet" => {:states => {"$each" => states}}}, new: true)
    end

    states = states.collect { |state| state.serializable_hash }
    Customer.where('name' => 'John').find_and_modify({"$addToSet" => {:states => {"$each" => states}}}, new: true)
    assert_equal 6, Customer.first.states.size

    pp JSON.parse(Customer.first.to_json)
  end
end
Run options: 

# Running tests:

[1/1] CustomerTest#test_find_and_modify_with_$addToSet_and_$each
Mongoid::VERSION:3.1.5
Moped::VERSION:1.5.1
{"_id"=>"5273f65de4d30bd5c1000001",
 "name"=>"John",
 "states"=>
  [{"_id"=>"5273f65de4d30bd5c1000002", "name"=>"New Jersey"},
   {"_id"=>"5273f65de4d30bd5c1000003", "name"=>"New York"},
   {"_id"=>"5273f65de4d30bd5c1000006", "name"=>"Illinois"},
   {"_id"=>"5273f65de4d30bd5c1000007", "name"=>"Indiana"},
   {"_id"=>"5273f65de4d30bd5c1000004", "name"=>"Washington"},
   {"_id"=>"5273f65de4d30bd5c1000005", "name"=>"Oregon"}]}
Finished tests in 0.061650s, 16.2206 tests/s, 64.8824 assertions/s.              
1 tests, 4 assertions, 0 failures, 0 errors, 0 skips