0

I have two classes:

class User
  include Mongoid::Document

  has_one :preference
  attr_accessible :name
  field :name, type: String
end

class Preference
  include Mongoid::Document

  belongs_to :user
  attr_accessible :somepref
  field :somepref, type: Boolean
end

And I have two factories:

FactoryGirl.define do
  factory :user do
    preference
    name 'John'
  end
end

FactoryGirl.define do
  factory :preference do
    somepref true
  end
end

After I create a User both documents are saved in the DB, but Preference document is missing user_id field and so has_one relation doesn't work when I read User from the DB.

I've currently fixed it by adding this piece of code in User factory:

after(:create) do |user|
  #user.preference.save! #without this user_id field doesn't get saved
end

Can anyone explain to me why is this happening and is there a better fix?

1

2 Answers 2

1

Mongoid seems to be lacking support here.

When FactoryGirl creates a user, it first has to create the preference for that new user. As the new user does not have an id yet, the preference can't store it either.

In general, when you try create parent & child models in one operation, you need two steps:

  1. create the parent, persist to database so it get's an id.
  2. create the child for the parent and persist it.

Step two would end up in an after(:create) block. Like this:

FactoryGirl.define do
  factory :user do
    name 'John'

    after(:create) do |user|
       preference { create(:preference, user: user) }
    end 
  end
end
3
  • Thanks! Your code didn't work at first, but I've rewritten the line inside the block like this: user.preference = create(:preference, user: user) and it works.
    – xx77aBs
    Apr 2, 2014 at 12:37
  • Any idea what to do if I have validates_presence_of :preference in User class? Validation fails before after(:create) callback is executed.
    – xx77aBs
    Apr 2, 2014 at 12:40
  • Oops, yeah, I forgot the user. As to validations: you might need to use build instead of create.
    – scivi
    Apr 2, 2014 at 13:54
0

As stated in this answer:

To ensure that you can always immediately read back the data you just wrote using Mongoid, you need to set the database session options

consistency: :strong, safe: true

neither of which are the default.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.