-
Notifications
You must be signed in to change notification settings - Fork 21.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add first_or_create family of methods to Active Record #2757
Add first_or_create family of methods to Active Record #2757
Conversation
…uild to Active Record. This let's you write things like: User.where(:first_name => "Scarlett").first_or_create!(:last_name => "Johansson", :hot => true) Related to rails#2420.
Thanks, I will review this in due course but it might be a few days. |
|
||
<sql> | ||
SELECT * FROM clients WHERE (clients.first_name = 'Ryan') LIMIT 1 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
poor Ryan :D
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Haha
# | ||
# Expects arguments in the same format as <tt>Base.create</tt>. | ||
# | ||
# ==== Examples |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add a first example with no arguments to first_or_create
? (As I think this will be the most common usage.)
E.g.
User.where(:first_name => 'Scarlett').first_or_create
# => <User id: 1, first_name: 'Scarlett'>
Also, I would remove the examples of creating several users at the same time, as I think this is a kind of odd use case and only works because create
happens to be implemented that way.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Both things done in d03aff7.
…e and removing examples that create several users at the same time (this is confusing and not really helpful).
… family of methods.
@jonleighton @tenderlove I know you are busy with lots of stuff but can you take a quick look at this pull request? I think there's not much left to do before merging. |
Add first_or_create family of methods to Active Record
Merged, thanks! |
This pull request let's you write:
Take a look at #2420 for the discussion about this.