title: Grouping items that were created within a certain time of each other
body: <p>I have a bunch of products (500k or so) in a database that were created over the last several years and I'd like to group them together (Rails 2.3.14)</p>
  
  <p>Ideally, they would be considered the same group if:</p>
  
  <ol>
  <li>They were created by the same company_id</li>
  <li>They were created within 10 minutes of each other</li>
  </ol>
  
  <p>A rough pass at what I'm trying to accomplish:</p>
  
  <pre><code>def self.package_products
    Company.each do |company|
     package = Package.new
     products = Product.find(:all, :conditions =&gt; [:company_id = company &amp;&amp; created_around_similar_times])
     package.contents = first_few_product_descriptions
     package.save!
     products.update_all(:package_id =&gt; package.id)
   end
  end
  </code></pre>
  
  <p>To me it smells bad though. I don't like looping through the companies and can't help but think there's a better way to do it.  Does anyone have any sql-fu that can group similar items?  Basically looking to find products from the same company that were created within 10 minutes of each other and assign them the same package_id.</p>