以下の内容はhttps://kamatimaru.hatenablog.com/entry/2026/01/03/220307より取得しました。


RailsのActiveRecordでscopeを使う ※ 引数あり/なしパターン

RailsActiveRecordでscopeを使う方法です。

scopeとはよく使う条件をメソッド化できるActiveRecordの機能です。

引数なしのパターン

以下のように定義します。

class Restaurant < ApplicationRecord
  # 省略
  scope :south_india, -> { joins(:tags).where(tags: { name: "南インド" }) }
end

Restaurantモデルのメソッドとして呼び出すことができます。

app(dev):002> Restaurant.south_india.count
  Restaurant Count (8.6ms)  SELECT COUNT(*) FROM "restaurants" INNER JOIN "restaurants_tags" ON "restaurants_tags"."restaurant_id" = "restaurants"."id" INNER JOIN "tags" ON "tags"."id" = "restaurants_tags"."tag_id" WHERE "tags"."name" = '南インド' /*application='App'*/
=> 1

引数ありのパターン

引数を渡すこともできます。その場合は以下のように定義します。

class Restaurant < ApplicationRecord
  # 省略
  scope :has_tag, -> (name){ joins(:tags).where(tags: { name: name }) }
end

以下のようにscope呼び出し時に引数を渡すことができます。発行されているSQLは引数なしパターンと同じになっています。

app(dev):003> Restaurant.has_tag("南インド").count
  Restaurant Count (9.3ms)  SELECT COUNT(*) FROM "restaurants" INNER JOIN "restaurants_tags" ON "restaurants_tags"."restaurant_id" = "restaurants"."id" INNER JOIN "tags" ON "tags"."id" = "restaurants_tags"."tag_id" WHERE "tags"."name" = '南インド' /*application='App'*/
=> 1



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

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