title: Rails: What's the difference between lambda, scope and class method? What's best practice?
body: <p>The <code>code</code> snippet has three methods: <code>lambda</code>, <code>scope</code> and <code>class method</code>.</p>
  
  <p>All of them returns the same results.</p>
  
  <p><strong>Questions:</strong></p>
  
  <ol>
  <li>Is there any best practice in <code>Ruby/Rails</code> when it is preferred to use one over the other ?</li>
  <li><p>In what cases would you use <code>lambda</code>, <code>scope</code> or <code>class</code> method ( best practices ). </p>
  
  <pre><code>class Cars &lt; ActiveRecord::Base     
    attr_accessible :manufacturer, :price, :used
  
    #one
    scope :used_and_cheap_lambda, lambda { where('used = ?', true ).where('price &gt;= ?',30000) }
  
    #two
    scope :used_and_cheap_scope, where('used = ?', true ).where('price &gt;= ?',30000)
  
    #three
    def self.used_and_cheap_class
      where('used = ?', true ).where('price &gt;= ?',30000)
    end
  end
  
  
  
  
  
  Cars.used_and_cheap_lambda.count
  =&gt; #24
  
  Cars.used_and_cheap_class.count
  =&gt; #24
  
  Cars.used_and_cheap_scope.count
  =&gt; #24
  </code></pre></li>
  </ol>