2

I am having some trouble with factory_girl's associations:

Factory.define :account do |f|
  f.sequence(:email) {|n| "john#{n}@example.com" }
  f.password "secret"
  f.confirmed_at 2.days.ago.to_s(:db)
  f.name "John Doe"
  f.seller false
  f.admin false
end

Factory.define :request do |f|
  f.association :account
  f.message 'Test message.'
end

And here's the problem:

When I do a = Factory(:request), a.account => nil but when I do a = Factory.build(:request), a.account is correct.

Any idea why this might be ?

Thanks

3
  • 1
    No validation problem here? What if you do a.errors after the creation? Jul 19, 2011 at 8:47
  • there are no validation errors on the Request mock... it gets saved but with no account_id, which makes me wonder if there are validation errors on request.account, any way I can check for that ? Jul 19, 2011 at 10:30
  • I am also not sure why the SQL looks like this before the Request is created "SELECT accounts.* FROM accounts WHERE accounts.email IS NULL LIMIT 1" Jul 19, 2011 at 10:35

1 Answer 1

0

Factory(:request) calls for default strategy, did you changed that somewhere?

3
  • That's not an answer, just a comment Jul 19, 2011 at 9:22
  • factory_girl gives you the chance to override the default strategy used to build objects. In any case looking at the above code, are you sure you aren't missing the password_confirmation field too?
    – kain
    Jul 19, 2011 at 17:35
  • I just realized I was setting the association in the model in a before_save callback... ugh... thanks for help tho Jul 20, 2011 at 7:57

Your Answer

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

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