Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

Damon Clinkscales http://damonclinkscales.com/

« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS 

Grabbing IDs instead of hydrating a model object

Sometimes you just want the IDs of a set of objects instead of creating a bunch of model objects. It turns out this is easy to do.

For example, if we want to get back a list of tag ids in our Product model to do some processing/munging:

   1  
   2  def tag_ids
   3      sql = "SELECT tag_id FROM products_tags WHERE product_id = #{self.id} ORDER BY tag_id ASC"
   4      ActiveRecord::Base.connection.select_values(sql).collect! { |x| x.to_i }
   5  end


Voila, an array of integers. Much faster, depending on the size of your model object.
« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS