The code snippet has three methods: lambda, scope and class method.
All of them returns the same results.
Questions:
- Is there any best practice in
Ruby/Railswhen it is preferred to use one over the other ? In what cases would you use
lambda,scopeorclassmethod ( best practices ).class Cars < ActiveRecord::Base attr_accessible :manufacturer, :price, :used #one scope :used_and_cheap_lambda, lambda { where('used = ?', true ).where('price >= ?',30000) } #two scope :used_and_cheap_scope, where('used = ?', true ).where('price >= ?',30000) #three def self.used_and_cheap_class where('used = ?', true ).where('price >= ?',30000) end end Cars.used_and_cheap_lambda.count => #24 Cars.used_and_cheap_class.count => #24 Cars.used_and_cheap_scope.count => #24