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
SYS'
password and 'SID'
during setup—they’ll come in handy later.
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.
Download Oracle Instant Client
Download the right package for your OS from the Oracle Instant Client download page.
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.
Set Environment Variables
To make sure your system knows where to find the Instant Client, you’ll need to set some environment variables:
export LD_LIBRARY_PATH=/usr/local/oracle/instantclient:$LD_LIBRARY_PATH export PATH=/usr/local/oracle/instantclient:$PATH
> 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.
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
.
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
Time to create your Rails app and hook it up to Oracle.
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
Let’s create a model and migrate the database to see everything in action.
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
Now that everything’s set up, let’s play around with the Oracle database using the Rails console.
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
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.
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.
© Copyright Shadbox. All Rights Reserved
Rate Blog :
Share on :
Do you have any blog suggestion? please click on the link