Rubocop: not good advise for where.take

Created on 22 Nov 2016  路  2Comments  路  Source: rubocop-hq/rubocop

when i use where.take by greater than condition,rubocop advise i use find_by instead of them

C: Rails/FindBy: Use find_by instead of where.take.
        total_miles = where(car_number: car_no).where('update_time >= ?', Time.current - 0.5.hours).take&.total_miles
                                                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

RuboCop version

$ rubocop -V
0.45.0 (using Parser 2.3.1.4, running on ruby 2.3.1 x86_64-linux)
bug

Most helpful comment

Finally, I adopted the writi like

 total_miles = where('update_time >= ?', Time.current - 0.5.hours).find_by(car_number: car_no)&.total_miles

I think this is Rubocop meaning,thanks.

All 2 comments

Can you clarify how this is not good advice? The generated SQL should be the same. 馃榾

Finally, I adopted the writi like

 total_miles = where('update_time >= ?', Time.current - 0.5.hours).find_by(car_number: car_no)&.total_miles

I think this is Rubocop meaning,thanks.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Ana06 picture Ana06  路  3Comments

bbugh picture bbugh  路  3Comments

millisami picture millisami  路  3Comments

herwinw picture herwinw  路  3Comments

mikegee picture mikegee  路  3Comments