From 93fd780a882555f6ae369ca5a445a47568bc5f1a Mon Sep 17 00:00:00 2001 From: Cristian Planas Date: Mon, 1 Jul 2013 01:38:46 +0200 Subject: Using preferred find_by syntax in guides --- guides/source/action_controller_overview.md | 2 +- guides/source/action_mailer_basics.md | 2 +- guides/source/active_record_basics.md | 8 ++++---- guides/source/association_basics.md | 2 +- guides/source/layouts_and_rendering.md | 6 +++--- 5 files changed, 10 insertions(+), 10 deletions(-) (limited to 'guides/source') diff --git a/guides/source/action_controller_overview.md b/guides/source/action_controller_overview.md index 6a91418e1f..f1600fe458 100644 --- a/guides/source/action_controller_overview.md +++ b/guides/source/action_controller_overview.md @@ -410,7 +410,7 @@ class ApplicationController < ActionController::Base # logging out removes it. def current_user @_current_user ||= session[:current_user_id] && - User.find_by_id(session[:current_user_id]) + User.find_by(id: session[:current_user_id]) end end ``` diff --git a/guides/source/action_mailer_basics.md b/guides/source/action_mailer_basics.md index d420365ec0..87a08e8661 100644 --- a/guides/source/action_mailer_basics.md +++ b/guides/source/action_mailer_basics.md @@ -517,7 +517,7 @@ method. Here's an example: ```ruby class UserMailer < ActionMailer::Base def receive(email) - page = Page.find_by_address(email.to.first) + page = Page.find_by(address: email.to.first) page.emails.create( subject: email.subject, body: email.body diff --git a/guides/source/active_record_basics.md b/guides/source/active_record_basics.md index 1f25c6ae95..d9fb20f3bf 100644 --- a/guides/source/active_record_basics.md +++ b/guides/source/active_record_basics.md @@ -253,7 +253,7 @@ user = User.first ```ruby # return the first user named David -david = User.find_by_name('David') +david = User.find_by(name: 'David') ``` ```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. ```ruby -user = User.find_by_name('David') +user = User.find_by(name: 'David') user.name = 'Dave' user.save ``` @@ -279,7 +279,7 @@ A shorthand for this is to use a hash mapping attribute names to the desired value, like so: ```ruby -user = User.find_by_name('David') +user = User.find_by(name: 'David') user.update(name: 'Dave') ``` @@ -297,7 +297,7 @@ Likewise, once retrieved an Active Record object can be destroyed which removes it from the database. ```ruby -user = User.find_by_name('David') +user = User.find_by(name: 'David') user.destroy ``` diff --git a/guides/source/association_basics.md b/guides/source/association_basics.md index c0e584a1c7..884aa6a9ea 100644 --- a/guides/source/association_basics.md +++ b/guides/source/association_basics.md @@ -2171,7 +2171,7 @@ You're not limited to the functionality that Rails automatically builds into ass class Customer < ActiveRecord::Base has_many :orders do 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 diff --git a/guides/source/layouts_and_rendering.md b/guides/source/layouts_and_rendering.md index f6cac997ff..5908801bc9 100644 --- a/guides/source/layouts_and_rendering.md +++ b/guides/source/layouts_and_rendering.md @@ -592,7 +592,7 @@ def index end def show - @book = Book.find_by_id(params[:id]) + @book = Book.find_by(id: params[:id]) if @book.nil? render action: "index" end @@ -607,7 +607,7 @@ def index end def show - @book = Book.find_by_id(params[:id]) + @book = Book.find_by(id: params[:id]) if @book.nil? redirect_to action: :index end @@ -626,7 +626,7 @@ def index end def show - @book = Book.find_by_id(params[:id]) + @book = Book.find_by(id: params[:id]) if @book.nil? @books = Book.all flash.now[:alert] = "Your book was not found" -- cgit v1.2.3