Problem with attributes_for and associations

1,216 views
Skip to first unread message

John Moody

unread,
Mar 13, 2009, 4:33:21 PM3/13/09
to factory_girl
I have the following models:

class TwitterAccount < ActiveRecord::Base
belongs_to :user
...
end

class User < ActiveRecord::Base
include Clearance::App::Models::User
has_one :twitter_account
...
end

I've set up the following factories to test this relationship:

Factory.define :twitter_account do |twitter_account|
twitter_account.twitter_username { "twitterUsername" }
twitter_account.twitter_password { "twitterPassword" }
end

Factory.define :user do |user|
user.email { Factory.next :email }
user.password { "password" }
user.password_confirmation { "password" }
user.time_zone { "Pacific Time (US & Canada)" }
user.twitter_account {|a| a.association(:twitter_account) }

end

However, when the build-in Clearance tests (or my own) call
Factory.attributes_for(:user), the twitter_account attribute is set to
nil. (If I do Factory(:user), the twitter_account is set up
properly.) Is there any way to make attributes_for include the
associated object?

melvin ram

unread,
Mar 13, 2009, 10:45:22 PM3/13/09
to factory_girl
Have you though of using Factory.build instead of .attributes_for? I
think the attributes_for is just for getting the attributes... not the
associated objects

John Moody

unread,
Mar 14, 2009, 12:23:42 AM3/14/09
to factory_girl
It has to be attributes_for, as it's part of a functional test - the
attributes are sent as part of a POST.

Robert Wahler

unread,
Mar 26, 2009, 5:30:28 PM3/26/09
to factory_girl
Try this, it works for me in the same situation:

Factory.build(:user).attributes.symbolize_keys

-robert
Reply all
Reply to author
Forward
0 new messages