# Help with accessing DB for general purpose on Active Record

**URL:** <https://www.ruby-forum.com/t/help-with-accessing-db-for-general-purpose-on-active-record/108184>\
**Category:** Rails\
**Created:** [June 29, 2007, 8:54pm UTC](https://www.ruby-forum.com/t/help-with-accessing-db-for-general-purpose-on-active-record/108184 "2007-06-29T20:54:06Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![ssk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/82dd89/32.png) [@ssk](https://www.ruby-forum.com/u/ssk)\
**Post date:** [June 29, 2007, 8:54pm UTC](https://www.ruby-forum.com/t/help-with-accessing-db-for-general-purpose-on-active-record/108184/1 "2007-06-29T20:54:06Z")

</div>

Hello,

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  
rules.

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  
tables).  
I just want to use my sql statements for retrieval and update (select/  
insert/update…).  
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.

ActiveRecord::Base.establish\_connection(…)  
rows = ActiveRecord::Base.find\_by\_sql(“select \* from table1”)  
rows.each do |row|  
hash = row.attributes\_before\_type\_cast  
…  
end

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.

Thanks.

Sam
