Ruby on Rails Active Records - Ruby on Rails

Next step is to create Rails Active Records (Models). It is the Object/Relational Mapping (ORM) layer supplied with Rails. It closely follows the standard ORM model, which is-

  • tables map to classes,
  • rows map to objects and
  • columns map to object attributes.

Rails Active Records provide an interface and binding between the tables in a relational database and the Ruby program code that manipulates database records. Ruby method names are automatically generated from the field names of database tables.

Each Active Record object has five methods for database access such as Create, Read, Update, and Delete, known as CRUD in short. CRUD allows simple designs and straight forward mappings between database tables and application objects.

How to translate a Domain Model into SQL

This process is generally straight forward if you follow the following and write Rails-friendly SQL.

  • Each entity gets a table in the database named after it, but in the plural. For example, if the entity is book, it will be named as books.
  • Each such entity-matching table has a field called id. It contains a unique integer for each record inserted into the table.
  • Given entity x and entity y, if entity y belongs to entity x, then table y has a field called x_id.
  • The bulk of the fields in any table store the values for that entity's simple properties (anything that's a number or a string).

How to create Active Record Files (Models)

Use the following command from the top level of the application directory to create the Active Record files for our entities for library application.

The auto code of this rails generate model book will be

Auto code of generate model book

By doing so, you are telling the generator to create models called Book and Subject to store instances of books and subjects. Remember to capitalise Book and Subject and using the singular form. This is a Rails paradigm that you should follow each time you create a model.

When you use the generate tool, Rails creates the actual model file that holds all the methods unique to the model and the business rules you define, a unit test file for performing test-driven development, a sample data file (called fixtures) to use with the unit tests, and a Rails migration that makes creating database tables and columns easy.

Along with creating many other files and directories, this will create files named book.rb and subject.rb. These files contains a skeleton definition in the app/models directory.

Use the following command to know the content available in book.rb -

Use the following command to know the content available in subject.rb -

How to create Associations between Models

If there are multiple models in your rails application, you need to create connection between those models via associations. Active Record supports three types of associations -

  • one-to-one − A one-to-one relationship exists when one item has exactly one of another item. For example, a person has exactly one birthday or a dog has exactly one owner.
  • one-to-many − A one-to-many relationship exists when one item can be a member of many other items. For instance, one subject can have many books.
  • many-to-many − A many-to-many relationship exists when the first item is related to one or more of a second object, and the second object is related to one or many of the first object.

These associations are defined by adding declarations to your models:has_one, has_many, belongs_to, and has_and_belongs_to_many.

Next step is to tell Rails what relationships you want to establish within the library data system by modifying book.rb and subject.rb like -

A singular subject is used here as one Book can belong to a single Subject.

A plural subject, books, are used here because one subject can have multiple books.

How to implement Validations on Models

The implementation of validations is done in a Rails model. The data you are entering into the database is defined in the actual Rails model, so it only makes sense to define what valid data entails in the same location.

The validations are -

  • The value of title field should not be NULL.
  • The value of price field should be numeric.

Open book.rb in the appmodel subdiractory and put the following validations −

  • validates_presence_of − protects "NOT NULL" fields against missing user input.
  • validates_numericality_of − prevents the user, entering non numeric data.

Check Rails quick Guidefor more details.

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd Protection Status

Ruby on Rails Topics