Is Rails Deprecated?

Is the Rails find method deprecated?

In the old days, find(1) was the preferred way to search by id.

Is it better to use find_by(id:1) instead of find(1) ?

+5
source share
3 answers

None of the methods are deprecated (as far as I know). The difference between find and find_by() is that they return when the record does not exist. If the record with id 23 does not exist, this is what you get:

 Model.find(23) => ActiveRecord::RecordNotFound: Couldn't find Model with 'id'=23 

or

 Model.find_by(id: 23) => nil 

Using find_by more forgiving if you make queries where a nonexistent entry exists, because you get a nil value, not an exception.

+4
source

You probably mean the deprecated ActiveRecord :: Base # find as find (: first) and find (: all) , which are deprecated in favor of the first and all methods.

Support for these methods has been removed from rails 3.2 .

The method you used ( ActiveRecord :: FinderMethods # find ) is not deprecated.

+1
source

According to Rails 4.2.1, this is not so. Here is the API documentation for it (not official, but pretty good). I think you are looking at this method , which is deprecated in Rails 3.0.0.

I have never used the find_by() method. I believe the best / newer method to use is where() method, for example:

 User.where(name: "Bob", email: " bob@example.com ") 
0
source

Source: https://habr.com/ru/post/1232327/


All Articles