3

I'm trying to create a seed file to populate my mongo database. The following syntax creates a nil value when I try to define the array field value. I'm using mongoid v3.0.9. What am I doing wrong?

These following examples do not work when I put them in the seed file:

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)

I've defined the field in my class like so:

field :test_array_field, type: Array

2 Answers 2

7

Your second syntax works for me.

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

u = User.create roles: ['superadmin']
u.new_record? # => false
u.roles # => ["superadmin"]
Sign up to request clarification or add additional context in comments.

3 Comments

Can you try putting the User.create into a seed file and running it?
Can you do the reverse? Try it in your console?
I've already tried it in my console and it works like it works with you. Its only when its in a seed.rb file that it doesn't work.
4

Try this:

class Foo
  include Mongoid::Document

  field :bar, :type => Array, :default => []
  field :baz, :type => Hash, :default => {}
end

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.