以下の内容はhttps://kamatimaru.hatenablog.com/entry/2025/12/26/233623より取得しました。


Railsのfind, find_by, whereの使い分け

Railsfind, find_by, whereの使い分けをまとめます。

find

原則は以下の仕様です。

  • idで検索
  • 1件だけ取得 (SQLLIMIT 1がつく)
app(dev):001> Tag.find(1)
  Tag Load (2.9ms)  SELECT "tags".* FROM "tags" WHERE "tags"."id" = 1 LIMIT 1 /*application='App'*/
=>
#<Tag:0x0000ffff855660f0
 id: 1,
 created_at: "2025-11-03 00:17:07.271876000 +0000",
 name: "南インド",
 position: 1,
 updated_at: "2025-11-03 00:17:07.271876000 +0000",
 tag_group_id: 1>

引数にidを複数渡すとIN句で複数検索する挙動に変わります。

app(dev):002> Tag.find(1,2)
  Tag Load (2.5ms)  SELECT "tags".* FROM "tags" WHERE "tags"."id" IN (1, 2) /*application='App'*/
=>
[#<Tag:0x0000ffff846794c0
  id: 1,
  created_at: "2025-11-03 00:17:07.271876000 +0000",
  name: "南インド",
  position: 1,
  updated_at: "2025-11-03 00:17:07.271876000 +0000",
  tag_group_id: 1>,
 #<Tag:0x0000ffff84679380
  id: 2,
  created_at: "2025-11-03 00:42:55.770481000 +0000",
  name: "ビリヤニ",
  position: 3,
  updated_at: "2025-11-03 01:57:44.855153000 +0000",
  tag_group_id: 3>]

id以外のフィールドで検索しようとするとエラーになります。その場合はfind_byを使います。

app(dev):003> Tag.find(name: "南インド")
  Tag Load (4.3ms)  SELECT "tags".* FROM "tags" WHERE "tags"."id" = NULL LIMIT 1 /*application='App'*/
(app):3:in '<main>': Couldn't find Tag with 'id'={name: "南インド"} (ActiveRecord::RecordNotFound)

find_by

原則は以下の仕様です。

  • 指定したキーで検索
  • 1件だけ取得 (SQLLIMIT 1がつく)
app(dev):004> Tag.find_by(name: "南インド")
  Tag Load (4.5ms)  SELECT "tags".* FROM "tags" WHERE "tags"."name" = '南インド' LIMIT 1 /*application='App'*/
=>
#<Tag:0x0000ffff84670f00
 id: 1,
 created_at: "2025-11-03 00:17:07.271876000 +0000",
 name: "南インド",
 position: 1,
 updated_at: "2025-11-03 00:17:07.271876000 +0000",
 tag_group_id: 1>

find_by_{フィールド名}という書き方もできます。

app(dev):005> Tag.find_by_name("南インド")
  Tag Load (0.8ms)  SELECT "tags".* FROM "tags" WHERE "tags"."name" = '南インド' LIMIT 1 /*application='App'*/
=>
#<Tag:0x0000ffff846758c0
 id: 1,
 created_at: "2025-11-03 00:17:07.271876000 +0000",
 name: "南インド",
 position: 1,
 updated_at: "2025-11-03 00:17:07.271876000 +0000",
 tag_group_id: 1>
app(dev):006>

以下のように配列を渡すと、IN句で検索しますがfindと違ってLIMIT 1の条件は残るので、複数検索の用途には使えません。後述のwhereを使います。

app(dev):007> Tag.find_by(name: ["南インド","ビリヤニ"])
  Tag Load (3.1ms)  SELECT "tags".* FROM "tags" WHERE "tags"."name" IN ('南インド', 'ビリヤニ') LIMIT 1 /*application='App'*/
=>
#<Tag:0x0000ffff871508d8
 id: 2,
 created_at: "2025-11-03 00:42:55.770481000 +0000",
 name: "ビリヤニ",
 position: 3,
 updated_at: "2025-11-03 01:57:44.855153000 +0000",
 tag_group_id: 3>

where

以下の仕様です。

  • 指定したキーで検索
  • 配列で取得
app(dev):009> Tag.where(name: "南インド")
  Tag Load (4.8ms)  SELECT "tags".* FROM "tags" WHERE "tags"."name" = '南インド' /* loading for pp */ LIMIT 11 /*application='App'*/
=>
[#<Tag:0x0000ffff84676680
  id: 1,
  created_at: "2025-11-03 00:17:07.271876000 +0000",
  name: "南インド",
  position: 1,
  updated_at: "2025-11-03 00:17:07.271876000 +0000",
  tag_group_id: 1>]

まとめ

  • 1件取得したい:
    • 検索キーがid: findを使う
    • 検索キーがid以外: find_byを使う
  • 複数件取得したい: whereを使う



以上の内容はhttps://kamatimaru.hatenablog.com/entry/2025/12/26/233623より取得しました。
このページはhttp://font.textar.tv/のウェブフォントを使用してます

不具合報告/要望等はこちらへお願いします。
モバイルやる夫Viewer Ver0.14