I have an ActiveRecord Language model, with id and short_code (there are other columns, but they are not relevant to this issue). I want to create a method that will be provided with a list of shortcodes and return a list of identifiers. I do not need associations, I just need to finish the array, which 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 it wastes time / memory / processing.
My question is twofold:
- Is there a way to trigger ActiveRecord discovery that simply returns an array of a specific column of the table, rather than instantiating objects?
- If so, is it really worth collecting an array of length n rather than creating instances of n ActiveRecord objects?
Note that for my specific purpose, n will be approximately 200.
source share