Skip to content

Instantly share code, notes, and snippets.

@blueplanet
Created December 9, 2014 06:00
Show Gist options
  • Save blueplanet/629d2605096162e4a512 to your computer and use it in GitHub Desktop.
Save blueplanet/629d2605096162e4a512 to your computer and use it in GitHub Desktop.
FactoryGirlのattributes_forがassociationを生成してくれない ref: http://qiita.com/blueplanet/items/82a349f99d0faa925a85
# model
class Group
has_many :users
end
class User
belongs_to :group
validates :group_id, presence: true
end
# factory
FactoryGirl.define do
factory :user do
sequence(:name) {|n| "user #{n}" }
group
end
end
user_params = FactoryGirl.attributes_for(:user)
user_params = FactoryGirl.build(:user).attributes
user = FactoryGirl.build(:user)
user_params = user.attributes.merge(password: user.password, password_confirmation: user.password_confirmation)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment