Created
September 10, 2011 00:16
-
-
Save mejibyte/1207679 to your computer and use it in GitHub Desktop.
New syntax for Active Record.
This file contains hidden or 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.where(:first_name => 'Scarlett').first_or_create!(:last_name => "Johansson", :hot => true) | |
# => #<User id: 1, first_name: "Scarlett", last_name: "Johansson", hot: true> | |
User.where(:first_name => 'Scarlett').first_or_create!(:last_name => "O'Hara", :hot => false) | |
# => #<User id: 1, first_name: "Scarlett", last_name: "Johansson", hot: true> |
This file contains hidden or 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.find_or_create_by_first_name!(:first_name => 'Scarlett', :last_name => "Johansson", :hot => true) | |
# => #<User id: 1, first_name: "Scarlett", last_name: "Johansson", hot: true> |
No. The output is correct.
So with your first example you create Scarlett but on 2nd attempt because Scarlett already exists it returns the first Scarlett found, right?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
2nd example will output the following "last_name: "O'Hara", hot: false"
;)