Factory_girl: build associated object

I am using factory girl to build the following objects: user and
blog. A user has one blog. A blog belongs to a user.

When I do the following

Factory.define :user do |u|
u.association :blog
end

I get this error: Mysql::Error: Column ‘user_id’ cannot be null: . I
suspect that is because factory tries to build a blog before it saves
the associated user object. I am getting around this problem by
creating the associated blog in the steps file:

Given /^"([^"]*)" is a screen name of a user$/ do |user_screen_name|
user = User.find_by_screen_name(user_screen_name)
if user.nil?
user = Factory.create(:user, :screen_name => user_screen_name)
Factory.create(:blog, :user_id => user.id)
end
end

This works but I don’t think it’s very elegant. What do you think?
Thanks.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs