title: RAILS: Using scopes to return counts and compute a percentage for a user
body: <pre><code>Class Order &lt; ActiveRecord::Base
  
    scope :ordered, lambda { |user| where("orders.user_id = ?", user.id) }
    scope :delivered, lambda { |user| where("orders.user_id = ? and orders.delivered = ?", user.id, true) }
  
    def self.delivered_percentage (user)
      ((self.ordered(user).count/self.delivered(user).count) * 100)
    end
  
  end
  </code></pre>
  
  <p>I have an Order model, and I need to find out </p>
  
  <ol>
  <li>The count of ordered items</li>
  <li>The count of delivered items</li>
  <li>The percentage of delivered items.</li>
  </ol>
  
  <p>The ordered and delivered methods return an array. How do I have them return a count, and compute the percentage of delivered items.</p>
  
  <p>In my controller I would like to </p>
  
  <pre><code>Order(current_user).delivered_percentage
  </code></pre>
  
  <p>Thanks!</p>