How to Set Up and Use Ruby on Rails with Oracle Database

Total Blog Views: 161

Blog Status: publish

Created By: swaz_ahmed Created at: 08-20-2024

Tags: rubyonrails Oracle RailsSetup Database Integration Oracle Instant Client Ruby-OCI8 Database Configuration Rails Migrations Rails Models Rails Console

Introduction

Ruby on Rails (Rails) is an awesome framework for building web applications, and while it usually pairs with databases like MySQL or PostgreSQL, you can absolutely use it with Oracle Database too! If you’re working in an enterprise environment, Oracle might be your go-to. In this guide, I’ll walk you through setting up Rails with Oracle Database, step by step.

Step 1: Install Oracle Database

First things first, let’s get Oracle Database up and running on your machine. We’ll focus on installing it locally on a Linux system, but the steps are similar for other OSes.

          1.Download Oracle Database

             Head over to the Oracle website and grab the latest version that matches your operating system.

          2.Install Oracle Database

  •   Follow the installation instructions provided by Oracle. Make sure to note down important details like your 'SYS' password and 'SID' during setup—they’ll come in handy later.
  •  Once the installation is done, make sure Oracle Database is up and running.

 

Step 2: Install Oracle Instant Client

   Rails needs a little help to talk to Oracle, and that’s where the Oracle Instant Client comes in.

  1. Download Oracle Instant Client
    Download the right package for your OS from the Oracle Instant Client download page.

  2. Install Oracle Instant Client
    Unzip the package to a directory of your choice, like /usr/local/oracle/instantclient on Linux or c:/oracle/instantclient on Windows.

  3. Set Environment Variables
    To make sure your system knows where to find the Instant Client, you’ll need to set some environment variables:

  •  On Linux:

 

 export LD_LIBRARY_PATH=/usr/local/oracle/instantclient:$LD_LIBRARY_PATH
 export PATH=/usr/local/oracle/instantclient:$PATH
  •  On Windows:

             > Add the Instant Client path to your PATH environment variable.

Step 3: Install Ruby and Rails

Now, let’s get Ruby and Rails installed if you don’t have them already.

      1. Install Ruby
          Use a version manager like rbenv or rvm to install Ruby:

 

 rvm install 3.0.0
 rvm global 3.0.0

       2. Install Rails
           Install Rails by running: 

 

 gem install rails

 

Step 4: Install the 'ruby-oci8' Gem 

To connect Rails to Oracle, you’ll need the ruby-oci8 gem.

  1. Install Dependencies
    Before installing the gem, make sure you have all the necessary dependencies. For example, on Linux, you might need ruby-devel or build-essential.

  2. Install ruby-oci8
    Go ahead and install the gem:

 

 gem install ruby-oci8

 

NOTE: After this you need to Create the Database and User using the Oracle Console.

To connect to the Oracle database as the SYSDBA (Database Administrator) using SQL*Plus, you can use the following command:

 

 sqlplus as sysdba

 

Step 5: Set Up a New Rails Application

Time to create your Rails app and hook it up to Oracle.

  1. Create a New Rails App

 

rails new my_oracle_app -d oracle

 This command creates a new Rails app that’s ready to use with Oracle.

     2. Configure database.yml

           Open up the config/database.yml file and update it with your Oracle database credentials:

 

default: &default
  adapter: oracle_enhanced
  username: your_username
  password: your_password
  host: localhost
  port: 1521
  database: XE  # or your SID
development:
  <<: *default
  database: your_development_db_name
test:
  <<: *default
  database: your_test_db_name
production:
  <<: *default
  database: your_production_db_name

Step 6: Generate Models and Migrate the Database

Let’s create a model and migrate the database to see everything in action.

  1. Generate a Model

 

 rails generate model User name:string email:string

    This creates a User model with 'name' and 'email' fields. 

      2. Migrate the Database

     Apply the migration to create the users table in your Oracle database:

 

rails db:migrate

 

Step 7: Demo Usage in Rails Console

Now that everything’s set up, let’s play around with the Oracle database using the Rails console.

  1. Start the Rails Console Open the Rails console by running:

 

rails console

 

     2 . Create a New Record

             Let’s create a new User

 

user = User.create(name: "John Doe", email: "[email protected]")

          This will insert a new row in the 'users' table.

 

  3. Find a Record

           Retrieve the user you just created: 

 

user = User.find_by(email: "[email protected]")
puts user.name  # Output: John Doe

 

   4. Update a Record

           Update the user’s name:
 

user.update(name: "Jane Doe")
puts user.name  # Output: Jane Doe

 

 5. Delete a Record

        Delete the user: 

 

user.destroy

 

6. Querying Multiple Records

        Find all users created in the last day: 

 

users = User.where("created_at >= ?", 1.day.ago)
users.each do |user|
  puts user.name
end

 

Conclusion

And that’s it! You’ve successfully set up a Ruby on Rails application with an Oracle database. You can now leverage Oracle’s powerful database features within your Rails app. Whether you’re building enterprise-level applications or just exploring, this setup opens up a lot of possibilities.

 


swaz_ahmed

I am swaz_ahmed blogger on shadbox. I am influencer,content writer,author and publisher. Feel free to ask me any question and suggestions.



Comments



Buy traffic for your website

About Shadbox

we have the “Get things executed” lifestyle at our place of work. There are not any excuses, no if’s or however’s in our dictionary. committed to navigating the ship of creativity to create cell answers, we resolve the real-lifestyles troubles of our clients and their clients. Our passion for work has won us many awards, year after 12 months.

Services

Downloads