Monday, September 28, 2009

Connect to database using ruby and ActiveRecord

To get connected you database from your ruby file just include the "ActiveRecord" inside your ruby file.

require 'rubygems'
require 'activerecord'

#establish the connection.
ActiveRecord::Base.establish_connection(
:adapter => "mysql",
:database => "database",
:username => "username",
:password => "password",
:host => "localhost"
)

#create a class inherit with you activerecord base class.
class User < ActiveRecord::Base
end

user = User.find(:first)
puts user
Or

class SomeThing < ActiveRecord::Base
set_table_name :users
end

user = SomeThing.find(:first)
puts user

That's It.

1 comment:

raju adla said...

can we see that the activerecord module contains what and which format plz