Is rails 4 find_by deprecated?
I would avoid cluttering your model with such methods. Also, Rails still provides the find_by
method. Thus I think a better solution would be
Model.find_by(username: 'whatever_username')
According to the Rails 4 Release Notes:
All dynamic methods except for find_by_... and find_by_...! are deprecated.
find_by
is not a dynamic method and therefore it's NOT DEPRECATED, and find_by_...
& find_by_...!
are dynamic, but still not deprecated as mentioned above.
So that means you still can use the original methods provided by Active Record without having to define your own:
Model.find_by_username(:username)
Model.find_by(username: 'value', age: 24)
If you want the functionality of the really deprecated finder methods you can either include the gem that they were moved into: activerecord-deprecated_finders. Or follow what the Rails 4 Release Notes say:
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!(...).