has_many and belongs_to associations in factory_girl
Asked Answered
P

1

13

I have these models that I'm trying to create factories for using factory_girl.

class Foo < ActiveRecord::Base
  belongs_to :baz
end

class Baz < ActiveRecord::Base
  has_many :foos
end

I'm not sure how to create the factories without creating a loop where the factories endlessly call each other.

Factory.define :foo do |f|
  f.after_create do |ff|
    ff.baz = Factory(:baz)
  end
end

Factory.define :baz do |f|
  f.after_create do |ff|
    ff.foos = [Factory.create(:foo)]
  end
end

I realize I can just leave out ff.foos = [Factory.create(:foo)] in the baz factory, but then in my baz tests I'm forced to used foo.baz instead of just using baz. Am I forced to use the baz object by fetching it out of a foo factory in my tests? Or is there a better way?

Patristic answered 6/7, 2011 at 17:49 Comment(0)
Q
9

See the Associations section of the Getting Started guide

Added

So, you need to use that syntax from that section, ie. in your Foo declaration you need:

Factory.define :foo do |f|
  f.association :baz
end

No after_create needed for a belongs_to association.

Quarters answered 6/7, 2011 at 17:54 Comment(2)
Yes, I've read the documentation. It does not really answer my question.Patristic
What if the foreign key cannot be null? You need an after_create callback then.Patristic

© 2022 - 2024 — McMap. All rights reserved.