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.

On Jul 8, 11:38 pm, Jobu [email protected] wrote:

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

On Jul 9, 5:31 am, Frederick C. [email protected]
wrote:

On Jul 8, 11:38 pm, Jobu [email protected] wrote:

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.

On 9 Jul 2008, at 15:23, Jobu wrote:

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

On Jul 9, 2008, at 10:23 AM, Jobu wrote:

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.

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 B. http://agileconsultingllc.com
[email protected]

On Jul 9, 10:41 am, Frederick C. [email protected]
wrote:

I’d like to write a data import script as a rake task. It would
results? There’s currently no ActiveRecord derived model in place for
the legacy tables.

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

Nope, that’s what I needed. Thanks.

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…