Optimisation Ruby On Rails Application Rails database best practices Write efficient and chainable scopes: Return an ActiveRecord::Relation. Filter data in the database. Sort in the database. Ordering out of scopes.