9

I heard that find_by is deprecated is this true? I have been thinking of alternatives such as creating for each find a different method, e.g.:

before:

Model.find_by_username 'username'

after: --in model---

class << self
   def by_username username
      where(:username => username).first 
   end
end

is it a good naming? what names do you give for such methods?

Update:

find_by is not deprecated but the announcement could have been clearer!

Community
  • 1
  • 1
reizals
  • 1,245
  • 1
  • 14
  • 21

3 Answers3

30

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!(...).
Joshua Pinter
  • 45,245
  • 23
  • 243
  • 245
Tamer Shlash
  • 9,314
  • 5
  • 44
  • 82
3

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')
Tamer Shlash
  • 9,314
  • 5
  • 44
  • 82
Bart Jedrocha
  • 11,450
  • 5
  • 43
  • 53
  • 3
    @TamerShlash No it isn't. Please read the [release notes](http://guides.rubyonrails.org/4_0_release_notes.html#active-record-deprecations) – Bart Jedrocha May 28 '14 at 21:12
-3

It was deprecated in favour of

Model.find_by username: 'username'

so you should probably use this version.

edariedl
  • 3,234
  • 16
  • 19
  • @Iceman No it isn't. Please read the [release notes](http://guides.rubyonrails.org/4_0_release_notes.html#active-record-deprecations) – Bart Jedrocha May 28 '14 at 21:12
  • @Iceman no it isn't. `find_by_*` are deprecated not `find_by`. `find_by_*` methods was moved into `deprecated-finders` gem and `find_by` is still absolutely ok. – edariedl May 28 '14 at 21:16
  • @Iceman check the [Rails Guides](http://guides.rubyonrails.org/active_record_querying.html#find-by) as further evidence that `find_by` was not depreciated. – Joe Kennedy May 28 '14 at 21:22
  • 1
    @JKen13579 While it's true that `find_by` is not deprecated. The rails guide still includes documentation for some deprecated methods like mentioned [here](http://guides.rubyonrails.org/active_record_querying.html#find-or-build-a-new-object). – Tamer Shlash May 28 '14 at 21:25
  • @TamerShlash very good point. Although anything that's deprecated is marked with that yellow background warning. – Joe Kennedy May 28 '14 at 21:29