LoginSignup
10
10

More than 5 years have passed since last update.

Concernsに切り出したmoduleのなかでActiveRecordのScopeを使う方法

Posted at

こんにちは! Rails芸人(仮)の@srockstyleです!

concern内にあるmoduleに切り出したメソッドでScopeつかいたーい、なんて時に使う方法です。

module TestModule
    extend ActiveSupport::Concern
    def self.included(base)
        base.class_eval do
            scope :testerexe, -> do 
                where(deleted_at: nil)
            end

            scope :testabexe, -> do 
                where(created_at: nil)
            end

            scope :testcdexe, -> do 
                where(updated_at: nil)
            end
        end
    end

    ## ここでScope読み込めちゃう
    def self.test
        User.testcdexe
    end
end 

複数モデルから参照するときは、whereなどでつかうカラムがあったりなかったりすることに気をつけて下さい。

以上、あまりRailにのってないかもしれないConcernの使い方でした。

10
10
2

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
10
10