2

mongoデータベースにデータを入力するためのシードファイルを作成しようとしています。次の構文はnil、配列フィールドの値を定義しようとすると値を作成します。私はmongoidv3.0.9を使用しています。私は何が間違っているのですか?

これらの例は、シードファイルに入れると機能しません。

User.create(name:'name', test_array_field:'[123,123]')
User.create(name:'name', test_array_field:[123,123])
User.create(name:'name', test_array_field:[123,123].to_a)

クラスのフィールドを次のように定義しました。

field :test_array_field, type: Array
4

2 に答える 2

6

あなたの2番目の構文は私にとってはうまくいきます。

class User
  field :roles, type: Array, default: []
end

u = User.create roles: ['superadmin']
u.new_record? # => false
u.roles # => ["superadmin"]
于 2012-10-28T21:35:52.940 に答える
3

これを試して:

class Foo
  include Mongoid::Document

  field :bar, :type => Array, :default => []
  field :baz, :type => Hash, :default => {}
end
于 2014-04-09T12:35:10.103 に答える