ActiveRecord with Ruby alone

Active Record is the main tool that Rails developers use to communicate with and underlying database. Active Record does some wonderful things for a web developer looking for abstractions in database setup, SQL connections and queries. We can get a cool command line Ruby application or utility script for our daily tasks using Active Record going in about 5 minutes!

First of all, get the activerecord gem using

  gem install activerecord

At the top of Ruby program we need to require the Active Record gem previously installed. Interacting with your database will be a pleasure now!

  require 'activerecord'

Establishing a connection to database

  ActiveRecord::Base.establish_connection {
    adapter:  "mysql",
    host:     "localhost",
    username: "root",
    password: "password",
    database: "dbname"
  }
Read More...

Interacting to MySQL using Ruby

This tutorials guides you to go straight into interacting with MySQL database using Ruby alone. No Rails. It is assumed that MySQL has been installed in your computer.

Now let’s get started:

First of all, lets begin with installing MySQL libraries via RubyGems

  gem install mysql

There is documentation for the MySQL library online . So you can follow that.

Now let’s fire irb.

  require 'mysql'
  db = Mysql.new('localhost', 'user', 'password', 'database')
Read More...