lundi 8 février 2016

Factory for dependent relations in rails app

I have following Models defined with their relations to each other

class Country < ActiveRecord::Base
  has_many :states, :dependent => :destroy
  has_many :users
end

class State < ActiveRecord::Base
  belong_to :country
  has_many :users
end

class User < ActiveRecord::Base
  belongs_to :person
  belongs_to :state
  belongs_to :country
end

I am having problem defining the User Factory, since it and Country both depend on State.

What is the best way to define dependent Factory in this case

Edit: Current Factory definitions are

Factory.define :user do |u|
  u.state "active"
  u.association :country
  u.association :state
end
Factory.define :country do |c|
  c.name 'Test Country'
  c.abbr 'US' ##Uniq      
end
Factory.define :state do |s|
  s.name 'Test State'
  s.association :country
end

Aucun commentaire:

Enregistrer un commentaire