My Voice

Ruby on Rails and Database

Ruby on Rails is a Web application framework used for developing Web applications. If you expect or want a user to enter information through a Web form, you require a database to store that information. In Rails framework, the database table has a plural name (ending with ‘s’), and the primary key in the database is known as id and auto-incremented. To retrieve stored information from the database, Rails uses a component named ActiveRecord that functions as a bridge between the database and Ruby code. ActiveRecord:

ActiveRecord is an Object/Relational Mapping layer available with Rails. It follows standard ORM rules such as

  • Columns map to object attributes
  • Rows map to objects and
  • Tables map to classes

Each ActiveRecord object has Create, Read, Update and Delete methods for database access. This characteristic allows Ruby on Rails applications to have straightforward mappings between applications objects and database tables. ActiveRecord does away with the need to use SQL in most cases. It is compatible with different databases such as MySQL, SQLite, and PostgreSQL. Irrespective of the database you are using, ActiveRecord method format remains the same. Ruby on Rails has well-defined relationships that reduce the need of writing low-level queries for the database. Let’s learn how to use ActiveRecord to perform queries to database. Working with the Database in Rails

Retrieve objects from the database:

ActiveRecord has many finder methods. These methods allow querying of database by passing arguments. The finder methods eliminate the need to write raw SQL for the purpose. Some of the finder methods are:

  • from
  • having
  • including
  • joins
  • none
  • select
  • where

The above finder methods return an instance of ActiveRecord:Relation Retrieve a single object:

ActiveRecord presents you with five different ways to retrieve a single object:

  • Using a primary key:

The Model.find(primary_key) retrieves an object corresponding to the value of the primary key.

For example
client = Client.find(10) find client with primary id=10 If the Model.find(primary_key) does not find primary key with the corresponding value 10, it will return an instance of ActiveRecord::RecordNotFound

  • Take:

Model.take retrieves an object without any conditional parameters or primary key client = Client.take

It will retrieve the object in the table with id=1. The retrieved object will vary with the engine used. Model.take will return Nil if no record is found in the table

  • First:

Model.first retrieves the first record with primary key client = Client.first

Model.first will raise an instance of ActiveRecord::RecordNotFound, if it does not find a matching record

  • Last:

Model.last retrieves the last record with primary key client = Client.last

Model.last will raise an instance of ActiveRecord::RecordNotFound, if it does not find a matching record

  • Find_by!:

Model.find_by! Searches for the first record in the table matching some conditions Client.find_by! first_name: ‘Harry’ it will retrieve a record which has Harry in the first_name field. Model.find_by! will raise an instance of ActiveRecord::RecordNotFound, if it does not find a matching record Types of Databases on Rails:

Ruby on Rails recommends the creation of three databases development, production and test. As per the convention their name should be

  • library_development
  • library_production
  • library_test

Before using the database, you need to initialize them and create a username and password for each database having full read and write permissions. The information about the databases is written in database.yml file located in the C:\ruby\library\config

Leave a Comment

Your email address will not be published. Required fields are marked *

*