mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Merge pull request #11205 from Gawyn/using-preferred-find_by-syntax-in-guides
Using preferred find_by syntax in guides
This commit is contained in:
commit
1feab8dac5
5 changed files with 10 additions and 10 deletions
|
@ -410,7 +410,7 @@ class ApplicationController < ActionController::Base
|
||||||
# logging out removes it.
|
# logging out removes it.
|
||||||
def current_user
|
def current_user
|
||||||
@_current_user ||= session[:current_user_id] &&
|
@_current_user ||= session[:current_user_id] &&
|
||||||
User.find_by_id(session[:current_user_id])
|
User.find_by(id: session[:current_user_id])
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
```
|
```
|
||||||
|
|
|
@ -517,7 +517,7 @@ method. Here's an example:
|
||||||
```ruby
|
```ruby
|
||||||
class UserMailer < ActionMailer::Base
|
class UserMailer < ActionMailer::Base
|
||||||
def receive(email)
|
def receive(email)
|
||||||
page = Page.find_by_address(email.to.first)
|
page = Page.find_by(address: email.to.first)
|
||||||
page.emails.create(
|
page.emails.create(
|
||||||
subject: email.subject,
|
subject: email.subject,
|
||||||
body: email.body
|
body: email.body
|
||||||
|
|
|
@ -253,7 +253,7 @@ user = User.first
|
||||||
|
|
||||||
```ruby
|
```ruby
|
||||||
# return the first user named David
|
# return the first user named David
|
||||||
david = User.find_by_name('David')
|
david = User.find_by(name: 'David')
|
||||||
```
|
```
|
||||||
|
|
||||||
```ruby
|
```ruby
|
||||||
|
@ -270,7 +270,7 @@ Once an Active Record object has been retrieved, its attributes can be modified
|
||||||
and it can be saved to the database.
|
and it can be saved to the database.
|
||||||
|
|
||||||
```ruby
|
```ruby
|
||||||
user = User.find_by_name('David')
|
user = User.find_by(name: 'David')
|
||||||
user.name = 'Dave'
|
user.name = 'Dave'
|
||||||
user.save
|
user.save
|
||||||
```
|
```
|
||||||
|
@ -279,7 +279,7 @@ A shorthand for this is to use a hash mapping attribute names to the desired
|
||||||
value, like so:
|
value, like so:
|
||||||
|
|
||||||
```ruby
|
```ruby
|
||||||
user = User.find_by_name('David')
|
user = User.find_by(name: 'David')
|
||||||
user.update(name: 'Dave')
|
user.update(name: 'Dave')
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -297,7 +297,7 @@ Likewise, once retrieved an Active Record object can be destroyed which removes
|
||||||
it from the database.
|
it from the database.
|
||||||
|
|
||||||
```ruby
|
```ruby
|
||||||
user = User.find_by_name('David')
|
user = User.find_by(name: 'David')
|
||||||
user.destroy
|
user.destroy
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
|
@ -2171,7 +2171,7 @@ You're not limited to the functionality that Rails automatically builds into ass
|
||||||
class Customer < ActiveRecord::Base
|
class Customer < ActiveRecord::Base
|
||||||
has_many :orders do
|
has_many :orders do
|
||||||
def find_by_order_prefix(order_number)
|
def find_by_order_prefix(order_number)
|
||||||
find_by_region_id(order_number[0..2])
|
find_by(region_id: order_number[0..2])
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -592,7 +592,7 @@ def index
|
||||||
end
|
end
|
||||||
|
|
||||||
def show
|
def show
|
||||||
@book = Book.find_by_id(params[:id])
|
@book = Book.find_by(id: params[:id])
|
||||||
if @book.nil?
|
if @book.nil?
|
||||||
render action: "index"
|
render action: "index"
|
||||||
end
|
end
|
||||||
|
@ -607,7 +607,7 @@ def index
|
||||||
end
|
end
|
||||||
|
|
||||||
def show
|
def show
|
||||||
@book = Book.find_by_id(params[:id])
|
@book = Book.find_by(id: params[:id])
|
||||||
if @book.nil?
|
if @book.nil?
|
||||||
redirect_to action: :index
|
redirect_to action: :index
|
||||||
end
|
end
|
||||||
|
@ -626,7 +626,7 @@ def index
|
||||||
end
|
end
|
||||||
|
|
||||||
def show
|
def show
|
||||||
@book = Book.find_by_id(params[:id])
|
@book = Book.find_by(id: params[:id])
|
||||||
if @book.nil?
|
if @book.nil?
|
||||||
@books = Book.all
|
@books = Book.all
|
||||||
flash.now[:alert] = "Your book was not found"
|
flash.now[:alert] = "Your book was not found"
|
||||||
|
|
Loading…
Reference in a new issue