Skip to content

Instantly share code, notes, and snippets.

@mejibyte
Created September 10, 2011 00:16
Show Gist options
  • Save mejibyte/1207679 to your computer and use it in GitHub Desktop.
Save mejibyte/1207679 to your computer and use it in GitHub Desktop.
New syntax for Active Record.
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>
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>
@olimart
Copy link

olimart commented Oct 16, 2011

2nd example will output the following "last_name: "O'Hara", hot: false"
;)

@mejibyte
Copy link
Author

No. The output is correct.

@olimart
Copy link

olimart commented Oct 16, 2011

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