Skip to content

Instantly share code, notes, and snippets.

@coryschires
Created August 5, 2011 20:24
Show Gist options
  • Select an option

  • Save coryschires/1128430 to your computer and use it in GitHub Desktop.

Select an option

Save coryschires/1128430 to your computer and use it in GitHub Desktop.
factory_girl_question.rb
# Having trouble getting nested factories working. Is this not supported? Seems like it would be.
#
# The associations are definitely setup correctly in the models:
# A campaign has_many :placements and a placement has_many :deliveries
#
@campaign = Factory(:campaign, :placements => [
Factory(:placement, :agency_rate => 3.00, :deliveries => [
Factory(:delivery, :quantity => 600),
Factory(:delivery, :quantity => 100)
]),
Factory(:placement, :agency_rate => 2.00, :deliveries => [
Factory(:delivery, :quantity => 150),
Factory(:delivery, :quantity => 200)
])
])
@coryschires
Copy link
Author

And this works but it's lame:

@campaign = Factory(:campaign)
placement_1 = Factory(:placement, :agency_rate => 3.00, :campaign => @campaign)
placement_2 = Factory(:placement, :agency_rate => 2.00, :campaign => @campaign)
Factory(:delivery, :placement => placement_1, :quantity => 600)
Factory(:delivery, :placement => placement_1, :quantity => 100)
Factory(:delivery, :placement => placement_2, :quantity => 150)
Factory(:delivery, :placement => placement_2, :quantity => 200)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment