# Rails:Using Insert statement in active record?

**URL:** <https://www.ruby-forum.com/t/rails-using-insert-statement-in-active-record/196748>\
**Category:** Rails\
**Created:** [July 19, 2008, 11:02pm UTC](https://www.ruby-forum.com/t/rails-using-insert-statement-in-active-record/196748 "2008-07-19T23:02:41Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![anukulsinghal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/dbc845/32.png) [@anukulsinghal](https://www.ruby-forum.com/u/anukulsinghal)\
**Post date:** [July 19, 2008, 11:02pm UTC](https://www.ruby-forum.com/t/rails-using-insert-statement-in-active-record/196748/1 "2008-07-19T23:02:41Z")

</div>

Hi,

I know this would be a simple solution to provide for any one of you. I  
am new to active record and this is what I have currently done:

require ‘rubygems’  
require ‘active\_record’

puts “connecting to db…”

ActiveRecord::Base.establish\_connection(  
:adapter =\> “mysql”,  
:host =\> “localhost”,

# :username =\> “myuser”,

```
:password => "schumi58",
:database => "test"

```

)

puts “connection established”

ActiveRecord::Schema.define do

drop\_table(:testTable)

create\_table(:testTable) do |t|  
t.column :name, :string  
end  
end

I am also referring to Active Record’s rdoc but am not able to make out  
how to use the insert statement in the above code. Can anyone help me in  
using INSERT statement in my code above?

Thanks,  
Anukul

---

<div class="post-metadata">

**Author:** ![anukulsinghal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/dbc845/32.png) [@anukulsinghal](https://www.ruby-forum.com/u/anukulsinghal)\
**Post date:** [July 20, 2008, 12:36pm UTC](https://www.ruby-forum.com/t/rails-using-insert-statement-in-active-record/196748/2 "2008-07-20T12:36:48Z")

</div>

On 19 Jul 2008, at 22:02, Anukul S. wrote:

> I am also referring to Active Record’s rdoc but am not able to make  
> out  
> how to use the insert statement in the above code. Can anyone help  
> me in  
> using INSERT statement in my code above?

Well if you’ve got activerecord model classes then you’d just create  
instances of those.  
If not ActiveRecord::Base.connection.insert will execute the chunk of  
sql you give it.

Fred
