Skip to content

Instantly share code, notes, and snippets.

@jeffdeville
Created December 6, 2012 16:19
Show Gist options
  • Select an option

  • Save jeffdeville/4225721 to your computer and use it in GitHub Desktop.

Select an option

Save jeffdeville/4225721 to your computer and use it in GitHub Desktop.
FactoryGirl associations failure
class User < ActiveRecord::Base
attr_accessible :name
has_one :address
end
class Address < ActiveRecord::Base
attr_accessible :addr1, :addr2
belongs_to :user
validates_presence_of :user
end
FactoryGirl.define do
factory :user do
name "MyString"
association :address
end
factory :address do
addr1 "MyString"
addr2 "MyString"
end
end
FactoryGirl.create :user
FactoryGirl.define do
factory :user do
name "MyString"
after(:build) do |user|
user.address = FactoryGirl.build :address
end
end
end
FactoryGirl.create :user, :address => FactoryGirl.build(:address, :addr1 => "elm st")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment