Note that there are some explanatory texts on larger screens.

plurals
  1. POValidation failed: [Object] code has already been taken
    primarykey
    data
    text
    <p>I newbie in FactoryGirl.</p> <p>I have a lot of FactoryGirl models. They are related each other.</p> <p>I declared the following models as below: </p> <pre><code>class Organization &lt; ActiveRecord::Base has_many :org_plan_years has_many :subscribers end class Subscriber &lt; ActiveRecord::Base belongs_to :organization end class OrgPlanYear &lt; ActiveRecord::Base belongs_to :organization has_many :coverages end class Coverage &lt; ActiveRecord::Base belongs_to :subscriber belongs_to :org_plan_year belongs_to :organization end </code></pre> <p>And define FactoryGirl: </p> <p><strong>factories/organizations.rb</strong></p> <pre><code>FactoryGirl.define do factory :organization_benefit, class Organization do name 'Benefit' organization_code '2341' end end </code></pre> <p><strong>factories/subscribers.rb</strong></p> <pre><code>FactoryGirl.define do factory :subscriber_875450002, class: Subscriber do association :organization, factory: :organization_benefit ssn 875450002 end end </code></pre> <p><strong>factories/org_plan_years.rb</strong></p> <pre><code>FactoryGirl.define do factory :org_plan_year_benefit, class: OrgPlanYear do association :organization, factory: :organization_benefit name 'First OPY' end end </code></pre> <p><strong>factories/coverages.rb</strong></p> <pre><code>FactoryGirl.define do factory :coverage_1, class: Coverage do association :subscriber, factory: :subscriber_875450002 association :org_plan_year, factory: :org_plan_year_benefit end end </code></pre> <p>Sample of a test:</p> <pre><code>it 'test' do FactoryGirl.create(:coverage_1) end </code></pre> <p>When I run my test, I get an error like this:</p> <pre><code>ActiveRecord::RecordInvalid: Validation failed: Organization code has already been taken </code></pre> <p>I think, the problem is caused because I set up organization several times in factories, but I can't solve the problem.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload