FactoryGirl issues with has_one and belongs_to associations

I fought with my associations for 3 solid days and don’t know where else to turn. I'm sure the problem is very simple, but I'm pretty new to Ruby on Rails, and that puzzled me ...

I created a User model that contains all of the credentials for Devise authentication. I have another profile model that contains all user settings (name, etc.). Finally, I have an address model that uses polymorphic associations associated with the profile.

User has_one Profile. Profile belongs_to User and has_one Address. An address is a polymorphic association that allows other models of my application to have an address associated with them.

At one point, I had all the FactoryGirl definitions, but I looked for the accepts_nested_attributes_for problem and added an after_initialize to create the user profile and profile address. Now my factories have a circular link to each other, and my rspec output is pierced:

 stack level too deep 

Since I have changed my configurations so much over the past few days, I think it's best to stop and ask for help. :) That's why I'm here. If anyone could help me, I would really appreciate it.

Here are my factory configurations:

Factory User

 FactoryGirl.define do sequence(:email) {|n| "person-#{n}@example.com"} factory :user do profile name 'Test User' email password 'secret' password_confirmation 'secret' # required if the Devise Confirmable module is used confirmed_at Time.now end end 

Factory Profile

 FactoryGirl.define do factory :profile do address company_name "My Company" first_name "First" last_name "Last" end end 

Factory Address

 FactoryGirl.define do factory :address do association :addressable, factory: :profile address "123 Anywhere" city "Cooltown" state "CO" zip "12345" phone "(123) 555-1234" url "http://mysite.com" longitude 1.2 latitude 9.99 end end 

Ideally, I would like to be able to test each factory independently. In my user model models, I would like to have a valid factory like this:

 describe "user" it "should have a valid factory" do FactoryGirl.create(:user).should be_valid end end describe "profile" it "should have a valid factory" do FactoryGirl.create(:profile).should be_valid end end describe "address" it "should have a valid factory" do FactoryGirl.create(:address).should be_valid end end 

What is secret sauce? I looked at wiki w770 and all over the Internet, but I'm afraid that I will not use the right conditions in my search. In addition, there are 4 different ways to do everything in FactoryGirl with mixed syntax in each search result that I came across.

Thanks in advance for your understanding ...

Update: 12/26/2012

I had Profile / User associations back. Instead of the user referring to the factory profile, I turned it over to have a Profile link for the factory user.

Here is the final factory implementation:

Factory User

 FactoryGirl.define do sequence(:email) {|n| "person-#{n}@example.com"} factory :user do #profile <== REMOVED THIS! name 'Test User' email password 'please' password_confirmation 'please' # required if the Devise Confirmable module is used confirmed_at Time.now end end 

Factory Profile

 FactoryGirl.define do factory :profile do user # <== ADDED THIS! company_name "My Company" first_name "First" last_name "Last" end end 

Factory Address

 FactoryGirl.define do factory :address do user association :addressable, factory: :profile address "123 Anywhere" city "Cooltown" state "CO" zip "90210" phone "(123) 555-1234" url "http://mysite.com" longitude 1.2 latitude 9.99 end end 

All tests pass!

+6
source share
1 answer

In accordance with the request for inquiry,

The profile empty. To link the user and the profile, you need to fill in the rest of the line and tell FactoryGirl that the user and the profile are connected.

+2
source

All Articles