scopeでfindやfind_by、firstやlastメソッドは使うべきではない
user.rb
scope :normal, -> (type) { where(type: type).first }
このようなscopeでnilを返すと、allが適用されてしまい、User.allが返ってしまう。
これを防ぐためには、scopeでは、ActiveRecord::Relationオブジェクトを返すようにする。
なので、ここでは、firstメソッドを付けずにwhereメソッドで取得する。
whereメソッドでは対象のデータない場合は、nilは返さず、[]を返す。
この[]はActiveRecord::Relationオブジェクト。
user.rb
scope :normal, -> (type) { where(type: type) }
def normal_record(type)
normal(type).first
end
もしくは、scopeの呼び出し先で、firstメソッドを使うのもあり。
normal(type).first