Forum: Ruby on Rails Help with accessing DB for general purpose on Active Record

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Ca0b18ec9e11dc777b2b8084fe5d5f90?d=identicon&s=25 Sam Kong (ssk)
on 2007-06-29 20:54
(Received via mailing list)

I'm converting data from a db to another.
It's kinda complex migration.
I need to merge some tables into one and change some column values by

I want to use ActiveRecord instead of DBI (For some reason, on my
ubuntu box, DBI for MySql doesn't work.)
Modeling is not important here.
And I don't want to define all models for every table (more than 800
I just want to use my sql statements for retrieval and update (select/
So basically, I'm not taking advantage of ActiveRecord.
I'm just using it as DB interface.

Through some research, I figured out how to retrieve data.

rows = ActiveRecord::Base.find_by_sql("select * from table1")
rows.each do |row|
  hash = row.attributes_before_type_cast

In this way, row.attributes didn't work but
attributes_before_type_cast did.

Question 1:
Is there a way to make row.attributes work? And for better, how can I
make row.col_name work?

Question 2:
How can I use insert/update statement?
For example, ActiveRecord::Base.insert_by_sql("insert into...")

I googled it already but couldn't find good examples.


This topic is locked and can not be replied to.