mirror of
https://github.com/jbranchaud/til
synced 2026-01-03 07:08:01 +00:00
Add Specify New Attributes For #find_or_create_by as a Rails til
This commit is contained in:
@@ -10,7 +10,7 @@ pairing with smart people at Hashrocket.
|
||||
|
||||
For a steady stream of TILs, [sign up for my newsletter](https://tinyletter.com/jbranchaud).
|
||||
|
||||
_1046 TILs and counting..._
|
||||
_1047 TILs and counting..._
|
||||
|
||||
---
|
||||
|
||||
@@ -690,6 +690,7 @@ _1046 TILs and counting..._
|
||||
- [Show Rails Models With Pry](rails/show-rails-models-with-pry.md)
|
||||
- [Show Rails Routes With Pry](rails/show-rails-routes-with-pry.md)
|
||||
- [Skip Validations When Creating A Record](rails/skip-validations-when-creating-a-record.md)
|
||||
- [Specify New Attributes For #find_or_create_by](rails/specify-new-attributes-for-find-or-create-by.md)
|
||||
- [Temporarily Disable strong_params](rails/temporarily-disable-strong-params.md)
|
||||
- [Test If An Instance Variable Was Assigned](rails/test-if-an-instance-variable-was-assigned.md)
|
||||
- [Truncate Almost All Tables](rails/truncate-almost-all-tables.md)
|
||||
|
||||
37
rails/specify-new-attributes-for-find-or-create-by.md
Normal file
37
rails/specify-new-attributes-for-find-or-create-by.md
Normal file
@@ -0,0 +1,37 @@
|
||||
# Specify New Attributes For #find_or_create_by
|
||||
|
||||
The ActiveRecord
|
||||
[`#find_or_create_by`](https://apidock.com/rails/v4.0.2/ActiveRecord/Relation/find_or_create_by)
|
||||
method is a handy way to get an object that represents a record. It will
|
||||
attempt to look up that record, usually based on a unique value or set of
|
||||
values. If it can find one, then that's the record you get. If nothing is
|
||||
found, then it will create a new record.
|
||||
|
||||
New records tend to need more data than just the unique lookup attribute. There
|
||||
are a couple ways these other attributes can be specified.
|
||||
|
||||
The first is by giving `#find_or_create_by` a block.
|
||||
|
||||
```ruby
|
||||
User.find_or_create_by(email: "some@email.com") do |new_user|
|
||||
new_user.admin = false
|
||||
new_user.password = params[:password]
|
||||
new_user.password_confirm = params[:password_confirm]
|
||||
end
|
||||
```
|
||||
|
||||
Another approach is to precede the `#find_or_create_by` call with a
|
||||
[`#create_with`](https://apidock.com/rails/ActiveRecord/QueryMethods/create_with)
|
||||
call.
|
||||
|
||||
|
||||
```ruby
|
||||
User.create_with(
|
||||
admin: false,
|
||||
password: params[:password],
|
||||
password_confirm: params[:password_confirm]
|
||||
).find_or_create_by(email: "some@email.com")
|
||||
```
|
||||
|
||||
In both cases, the extra attributes will not be applied to the `User` record in
|
||||
the case of a _find_; they are only used in the case of a _create_.
|
||||
Reference in New Issue
Block a user