Type a keyword and hit enter to start searching. Press Esc to cancel.

Posts Tagged ‘performance’

ActiveRecord is awesome! However, it’s not without a few downsides and common pitfalls. The simplicity sometimes comes at a cost of efficiency. A great example is the n+1 problem when scoping an ActiveRecord relation by a belongs_to association. For example, take the following associated Models class Employee < ActiveRecord::Base belongs_to :company end class Company < ActiveRecord::Base has_many :employees has_many :offices end If you were using an Employee instance method to find company offices for the Employee, you may do something like this: Office.where(company: self.company) Or you could say: Office.where(company_id: self.company_id) Both examples will give you the following query: SELECT * FROM offices […]