Tag Archives: rails

Checking record existence withActiveRecord

For some reason ActiveRecord does not optimize .any? or .empty? calls, they result in

SELECT COUNT(*)

query. For optimal performance one should use .exists? which is translated to

SELECT 1

query.

Few other hints can be found here: http://jontai.me/blog/2014/05/activerecord-use-efficient-queries-for-counting/

Advertisements