You may have already knowledge with the ruby on rails and how to work on it along with the Mysql. This tutorial is some what useful for the developers who wants to create an application using Rails with Oracle.
Why Need for Oracle ?
Every database has its own functionalities and unique speciality to handle data. Its depend upon the developer and the specifications were the main reason for shifting databases. The aim of this article is not to analyse those things and only to expose the knolwedge how to develop an application using oracle in ruby on rails. Let’s go !
Getting started
Its assumed that your system has Oracle already. Now , we need to install the driver for oracle get connected with the rails. Its also a open source driver which can be downloaded from the site.
Click the following link to get the driver oci
http://rubyforge.org/projects/ruby-oci8/
Make sure that whether you are getting the latest driver from there. Because lots of error might be araised due to the version problems. Download the file to your system. For eg: Assume the downloaded driver file is in C: drive. The ruby command to make run the driver is
c:\> ruby ruby-oci8-1.0.0-rc3-mswin32.rb
The ruby-oci8-1.0.0-rc3-mswin32 is the downloaded file name. so make sure the file name is correct.
Configuring oracle in rails :
Different databases require different connection properties. Rails is set up to work with MySQL by default, but you want to re-configure this project to use Oracle. Change the development properties as follows. (You could change the test and production properties also, but the scope of this article doesn’t include testing or production releases.)
development:adapter: ociusername: rubypassword: rubyhost: localhost
Open the file database.yml. As a rails programmer u must known that this file is responsible for connecting database in development , testing and in the production too. Now, in the development block make changes as per above. The username and password are the fields that you have to enter corresponding to your database.
Simple Application :
I’m going to show u a sample application running rails with oracle. Application name is product. Its a very simple application likes a product catalog. User of the application can add a product, edit a product and view the list of the product using scaffold. It is assume that the reader of this article were known the scaffold concept already.
To create an application
$ rails product
$ cd product
Now the product application is created. Now the database.yml file should be configured relative to our oracle database. Edit that file as shown in below the username and password field should to varied to your system.
development:
adapter: oci
username: admin
password: admin
host: localhost
Migration :
Migration is a concept that a rails developer should come across before move ahead. Create a migration file products by
$ ruby script/generate migration products
It will create a migration file 001_product.rb in the db\migrate path of the application.
open the file and edit it as shown below
class Product
def self.up
create_table :products do |t|
t.column :name, :string, :limit => 30
t.column :description, :string
t.column :price, :integer, :null => false, :default => 0
end
end
def self.down
end
end
Type the following command to make use od the migration.
$ rake db:migrate
Now the data base is connected to rails and a tabel products is also created for our application.
Scaffolding :
The scaffolding is the basic simple concept to create a application easily. Execute the following command to create a scaffold
$ ruby script/generate scaffold product
It will create codes automatically for the add, edit and list of the products. To run the application start the server as
$ ruby script/server
That’s all. Type the following URl in the browser to view the list of products in our products table which is in our oracle database.
http://localhost:3000/products/list
Initially the table is empty and it likes to be
Products adding through the application is advisible than directly add it to the database. Once some products are added it might be shown as