1. It seems everytime :section is created, the association :app_profile is created as well. Now, there is an existing uniqueness constraint to both :app_profile and :sections.

Say we have an app_profile for “Hotel 1″.

Now app_profile, “Hotel 1″, have two sections “Section 1″, “Section 2″.

Now the behaviour that we want is you cannot add “Section 1″ again. “Section 3″ is fine.

What is the factory definition that I need to specify for :section so that it will create an association if needed but will use an existing association if it already exists?

Validation failed: App name has already been taken (ActiveRecord::RecordInvalid)
./features/step_definitions/seed_steps.rb:6:in `/^there is a section called "([^"]*)"$/'
features/editing_existing_sections.feature:10:in `And there is a section called "Test Section 2"'
  1. Now I don’t know if this is ideal but the only way I worked around this is by assigning the factory build instance into an instance variable using “@”, thus it will be available through the whole request.

cucumber_factory_girl_association_thumb.png

Referene: Getting Started with factory_girl