Improve performance with ActiveRecord optimization