Access Data Outside of Active Record

I'd like to write a data import script as a rake task. It would execute a moderately complex query against some legacy tables returning a recordset. I'd like to get the results of that recordset into the rake task and iterate over it creating instances of ActiveRecord derived models.

Seems reasonable enough, but what's the question you're asking ?

Fred

I'd like to know how to do the first part. How do I execute a non- CRUD query against the legacy tables and receive a recordset of those results? There's currently no ActiveRecord derived model in place for the legacy tables.

I'd like to write a data import script as a rake task. It would execute a moderately complex query against some legacy tables returning a recordset. I'd like to get the results of that
recordset into the rake task and iterate over it creating instances of ActiveRecord derived models.

Seems reasonable enough, but what's the question you're asking ?

I'd like to know how to do the first part. How do I execute a non- CRUD query against the legacy tables and receive a recordset of those results? There's currently no ActiveRecord derived model in place for the legacy tables.

Have you played with ActiveRecord::Base.connection.select_all ?

Fred

Something like (assuming the legacy tables are in the same database):

If legacy_table has columns foo and bar

  foo bar   --- ---     1 one     2 two

   ActiveRecord::Base.connection.select_all("SELECT * FROM legacy_table") returns an array of hashes:    [{'foo'=>'1', 'bar'=>'one'}, {'foo'=>'2', 'bar'=>'two'}]

-Rob

Rob Biedenharn http://agileconsultingllc.com Rob@AgileConsultingLLC.com

If you can torture your query to the point where it will return records that mimic the model table, you could pass that into YourModel.find_by_sql. That may be convenient...

Nope, that's what I needed. Thanks.