Rails4で廃止になった findメソッド群

オレオレメモ。 正式には Dynamic finder methods と呼ぶらしい。

Rails3 から Rails4 にアップグレードすると

NoMethodError:
       undefined method `find_or_initialize_by_*****'

というエラーが出ることがある。

こういう時は

Ruby on Rails 4.0 Release Notes — Ruby on Rails Guides

を読むのが正解(だと思う)。

All dynamic methods except for find_by... and find_by...! are deprecated. Here's how you can rewrite the code:

find_all_by_... can be rewritten using where(...).

find_last_by_... can be rewritten using where(...).last.

scoped_by_... can be rewritten using where(...).

find_or_initialize_by_... can be rewritten using find_or_initialize_by(...).

find_or_create_by_... can be rewritten using find_or_create_by(...).

find_or_create_by_...! can be rewritten using find_or_create_by!(...).

この機能を戻したいときには、rails/activerecord-deprecated_finders · GitHub を使うとよい、とのこと。