views:

284

answers:

5

I have an ActiveRecord model Language, with columns id and short_code (there are other columns, but they are not relevant to this question). I want to create a method that will be given a list of short codes, and return a list of IDs. I do not care about associations, I just need to end up with an array that looks like [1, 2, 3, ...].

My first thought was to do something like

def get_ids_from_short_codes(*short_codes)
  Language.find_all_by_short_code(short_codes.flatten, :select => 'id').map(&:id)
end

but I'm not sure if that's needlessly wasting time/memory/processing.

My question is twofold:

  1. Is there a way to run an ActiveRecord find that will just return an array of a certain table column rather than instantiating objects?
  2. If so, would it actually be worthwhile to collect an array of length n rather than instantiating n ActiveRecord objects?

Note that for my specific purpose, n would be approximately 200.

+6  A: 

Honestly, for 200 records, I wouldn't worry about it. When you get to 2000, 20,000, or 200,000 records - then you can worry about optimization.

Make sure you have short_code indexed in your table.

If you are still concerned about performance, take a look at the development.log and see what the database numbers are for that particular call. You can adjust the query and see how it affects performance in the log. This should give you a rough estimate of performance.

Phil
Thanks, that makes sense. I guess I am just worried about instantiating objects being overkill when I just want one single column.
Daniel Vandersluis
+3  A: 

Agree with the previous answer, but if you absolutely must, you can try this

sql = Language.send(:construct_finder_sql, :select => 'id', :conditions => ["short_code in (?)", short_codes])
Language.connection.select_values(sql)

A bit ugly as it is, but it doesn't create in-memory objects.

neutrino
This works well, and is a LOT faster than the object route if you have thousands of records. Only downside is it doesn't work if you have WHERE conditions on included depencies (construct_finder_sql doesn't include joins)
Alexander Malfait
+3  A: 

Phil is right about this, but if you do find that this is an issue. You can send a raw SQL query to the database and work at a level below ActiveRecord. This can be useful for situations like this.

ActiveRecord::Base.connection.execute("SQL CODE!")

Benchmark your code first before you resort to this.

Ben Hughes
A: 

This really is a matter of choice.

Overkill or not, ActiveRecord is supposed to give you objects since it's an ORM. And Like Ben said, if you do not want objects, use raw SQL.

Swanand
A: 

if you're using associations you can get raw ids directly from ActiveRecord. eg.:

class User < ActiveRecord::Base
  has_many :users
end

irb:=> User.find(:first).user_ids
irb:>> [1,2,3,4,5]
Przemek Wroblewski