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|
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)
user = Factory.create(:user, :screen_name => user_screen_name)
Factory.create(:blog, :user_id => user.id)
This works but I don’t think it’s very elegant. What do you think?