title: Rails: Iterate over collection within a Model's Class Method
body: <p>I have a collection of <code>Foo</code> objects.  I want to create a class method that iterates over that collection and returns <code>true</code> if ALL objects in that collection have a certain value for an attribute.  Otherwise the class method returns <code>false</code>.</p>
  
  <p>I attempted the following but it does not work:</p>
  
  <pre><code>class Foo &lt; ActiveRecord::Base
  
    def self.all_have_number_value_999?
      each do |foo_object|
        return false if foo_object.number_val != 999
      end
      return true #all objects have #number_val == 999
    end
  
  end
  </code></pre>
  
  <p>Now assume I got a collection of <code>Foo</code> objects somehow.  I will make up how I got the collection of Foo objects, but the importance is that <code>number_value_999?</code> can iterate over that collection.  In other words: some unknown scope generated the collection of <code>Foo</code> objects before hand.  I just want to iterate over that collection <em>within</em> a <code>Foo</code> class method.</p>
  
  <pre><code>Foo.where(id: [123, 456]).all_have_number_value_999?
  </code></pre>
  
  <p>It is complaining in the class method because it doesn't know how to iterate over the collection.  So that is my question: How do I iterate over a collection within a model's class method?</p>