3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【Rails】scope、ActiveSupport::Concernについて

Last updated at Posted at 2020-01-23

環境

Rails 5.2.3
mysql 5.7.28

リテラルについて

例文

contoroller.rb
def index
  @boards = Board.includes(:user).order('created_at DESC')
end

def bookmarks
  @bookmarks = current_user.bookmarks.board.includes(:user).order('created_at DESC')
end

改善ポイント
・orderはリテラルではなくscopeで定義すべき

理由
・共通化できるクエリはscopeでメソッドのように定義すべき
・クエリはできるだけリテラルで使わないほうが良い
リテラル=変数やメソッド化していないただのベタ書きした文字や数字

メリット
・任意のメソッド名でどのような効果を持つかわかりやすくなる
・探す場所が1箇所に絞られ、リファクタリング容易(保守性)、データベースパフォーマンスの最適化(読み込みスピードUP)が図れる
・IDEの構文チェックがうまく走らないことがあり、ミスに気づけなくなることがある。

scope

共通的に使うクエリーをメソッドのように定義することができる便利な機能。

board.rb
scope :new_order, -> { order(created_at: :desc) }
contoroller.rb
def index
  @boards = Board.includes(:user).new_order
end

def bookmarks
  @bookmarks = current_user.bookmarks.board.includes(:user).new_order
end

このようにscopeを使用することで先ほどあげたメリットがある。
しかし、システムが大きくなるほどscopeの数が増えていき見通しが悪くなる。
そんな時にActiveSupport::Conernを使ってmoduleに機能を切り出す。

ActiveSupport::Concern

共通処理をモジュールとして切り出し、インクルードすることでモジュールを使用。

例文

board.rb
scope :new_order, -> { order(created_at: :desc) }
comment.rb
scope :new_order, -> { order(created_at: :desc) }

複数のモデルで同じscopeが定義されている時、ActiveSupport::Concernでまとめる。

引用

  1. app/models/concerns/以下のモジュールファイルを作成する
  2. ActiveSupport::Concernモジュールをextendで取り込む
  3. includedブロック内でscopeを宣言する
  4. モデル側でモジュールをincludeする
app/models/concerns/hoge.rb
require 'active_support'

module Hoge
  extend ActiveSupport::Concern

  included do
    scope :new_order, -> { order(created_at: :desc) }
  end
end
board.rb,comment共通
include Hoge

まとめ

参考資料
Rails: モデルの外では名前付きスコープだけを使おう(翻訳)
scopeをActiveSupport::Concernに分割する
[Rails] ActiveSupport::Concern の存在理由

3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?