Created
December 9, 2014 06:00
-
-
Save blueplanet/629d2605096162e4a512 to your computer and use it in GitHub Desktop.
FactoryGirlのattributes_forがassociationを生成してくれない ref: http://qiita.com/blueplanet/items/82a349f99d0faa925a85
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
user_params = FactoryGirl.attributes_for(:user) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
user_params = FactoryGirl.build(:user).attributes |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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