Getting a nil object when creating a has_many association

13 views
Skip to first unread message

Matthew Lang

unread,
Dec 6, 2009, 4:32:35 PM12/6/09
to factory_girl
I've got a factory that creates a record that has 2 has_many
associations, but I keep returning a nil object in one of my
associations. This is my first project using factory_girl and I just
wondered if I had my factory set-up correctly.

Factory.define :project_with_solutions, :class => Project do |f|
f.name 'Project with solutions'
f.benefits {|benefits| [benefits.association(:benefit)]}
f.solutions {|solutions| [solutions.association(:solution)]}
end

# Here's my test
@project = Factory.create(:project_with_solutions)
@benefit = @project.benefits.find(:first)
get :edit, :project_id => @project, :id => @benefit.id
assert_response :success

As you can see, I need to get an instance of a benefit as well as a
project. I'm getting a whiny nil error on my test, but I was under the
impression that the way my factory is setup, it would return saved
instances of a benefit record.

Any help on this is greatly appreciated.

Matthew

Matthew Lang

unread,
Jan 19, 2010, 6:07:37 PM1/19/10
to factory_girl
I managed to sort this out.

I needed to create the association starting from the child
association, so I created the association from the benefit and then I
was able to refer to a project without getting a whiny nil error.

For more information, see this thread:
http://groups.google.com/group/factory_girl/browse_thread/thread/2ffdd38eb31b0727

On Dec 6 2009, 9:32 pm, Matthew Lang <matt...@matthewlang.co.uk>
wrote:

Reply all
Reply to author
Forward
0 new messages