Tbpgr Blog

Employee Experience Engineer tbpgr(てぃーびー) のブログ

Ruby on Rails | Rails4 | 古いハッシュ形式のダイナミックファインダの廃止

概要

古いハッシュ形式のダイナミックファインダの廃止

内容

Rails4では古いハッシュ形式のダイナミックファインダの廃止になりました。

旧形式

Model.find_by_name "input_name"
Model.find_all_by_name "input_name"
Model.find(: all) "input_name"

推奨形式

Model.find_by name: value
Model.where name: value
Model.all

サンプル

前提DB

mysql> desc books;
+------------+--------------+------+-----+---------+----------------+
| Field      | Type         | Null | Key | Default | Extra          |
+------------+--------------+------+-----+---------+----------------+
| id         | int(11)      | NO   | PRI | NULL    | auto_increment |
| name       | varchar(255) | YES  |     | NULL    |                |
| isbn       | varchar(255) | YES  |     | NULL    |                |
| price      | int(11)      | YES  |     | NULL    |                |
| created_at | datetime     | YES  |     | NULL    |                |
| updated_at | datetime     | YES  |     | NULL    |                |
+------------+--------------+------+-----+---------+----------------+
6 rows in set (0.00 sec)

mysql> select * from books;
+----+----------------------------------------------------------------+----------------+-------+---------------------+---------------------+
| id | name                                                           | isbn           | price | created_at          | updated_at          |
+----+----------------------------------------------------------------+----------------+-------+---------------------+---------------------+
|  1 | RailsによるアジャイルWebアプリケーション開発                   | 978-4274068669 |  3200 | 2013-07-09 15:08:46 | 2013-07-09 15:08:46 |
|  2 | メタプログラミングRuby                                         | 978-4048687157 |  2800 | 2013-07-09 15:08:46 | 2013-07-09 15:08:46 |
|  3 | 入門bash                                                       | 978-4873112541 |  2800 | 2013-07-11 13:49:15 | 2013-07-11 13:49:15 |
+----+----------------------------------------------------------------+----------------+-------+---------------------+---------------------+
3 rows in set (0.00 sec)

推奨形式でデータ取得

User.find_by price: 2800
User.where price: 2800
User.all

Controllerの生成

$ bundle exec spring rake g controller sample show

controller

class SampleController < ApplicationController
  def show
    @find_by_book = Book.find_by price: 2800
    @find_all_by_book = Book.where price: 2800
    @all_book = Book.all
  end
end

view

%h1 Sample#show
%p Find me in app/views/sample/show.html.haml
%hr/ 
Book.find_by_book
%table 
  %tr 
    %th id
    %th name
    %th isbn
    %th price
    %th created_at
    %th updated_at
  %tr 
    = content_tag :td, @find_by_book.id
    = content_tag :td, @find_by_book.name
    = content_tag :td, @find_by_book.isbn
    = content_tag :td, @find_by_book.price
    = content_tag :td, show_dat(@find_by_book.created_at)
    = content_tag :td, show_at(@find_by_book.updated_at)

%hr/ 
Book.find_all_by_book
%table
  %tr 
    %th id
    %th name
    %th isbn
    %th price
    %th created_at
    %th updated_at
  -@find_all_by_book.each do |b|
    %tr 
      = content_tag :td, b.id
      = content_tag :td, b.name
      = content_tag :td, b.isbn
      = content_tag :td, b.price
      = content_tag :td, show_dat(b.created_at)
      = content_tag :td, show_at(b.updated_at)

%hr/ 
Book.all
%table 
  %tr 
    %th id
    %th name
    %th isbn
    %th price
    %th created_at
    %th updated_at
  -@all_book.each do |b|
    %tr 
      = content_tag :td, b.id
      = content_tag :td, b.name
      = content_tag :td, b.isbn
      = content_tag :td, b.price
      = content_tag :td, show_dat(b.created_at)
      = content_tag :td, show_at(b.updated_at)

結果