Author: Saim Khalid
-
Migrations
Rails Migration allows you to use Ruby to define changes to your database schema, making it possible to use a version control system to keep things synchronized with the actual code. Instead of writing schema modifications in pure SQL, you can use a Ruby Domain Specific Language (DSL) to make required changes to your table…
-
Active Record Query
Read this chapter to learn the different ways to retrieve data from the database using Active Record. You generally need to execute the SELECT query in raw SQL to retrieve data from the tables in a database of any type (such as MySQL, PostgreSQL, Oracle SQLite etc.). In most cases, the Rails ORM implemented by…
-
Active Record Associations
Relational databases have tables related to each other, and their relationship is established with primary and foreign keys. In Rails, the Active Record associations allow you to define relationships between models. Associations indicate how your models relate to each other. When you set up an association between models, Rails migration defines the Primary Key and…
-
Validation
It is important that the state of an object of Active Record class is validated before saving the same in the underlying database. This is essential to ensure the integrity of the data. For example, your model might have email as one of the attributes. It must be ensured that the user enters a valid…
-
Active Records
Rails Active Record is the Object/Relational Mapping (ORM) layer supplied with Rails. It closely follows the standard ORM model, which is as follows − 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…
-
Database Setup
Before starting with this chapter, make sure your database server is up and running. Ruby on Rails recommends to create three databases – a database each for development, testing, and production environment. According to convention, their names should be − You should initialize all three of them and create a user and password for them…
-
Examples
In this chapter, we will create a simple but operational online library system for holding and managing the books. This application has a basic architecture and will be built using two ActiveRecord models to describe the types of data that is stored − Workflow for Creating Rails Applications A recommended work flow for creating Rails…
-
Bundler
In Rails, Bundler is a dependency management tool that ensures your application has the correct versions of the required gems. A gem in Ruby is a packaged Ruby library that provides functionality for Ruby applications. Gems are hosted on RubyGems.org, which is the official Ruby package repository. The main role of Bundler is to manage and install…
-
Rails Console
Rails API provides a useful feature called Console. The console is an interactive tool for testing the code in our Rails application. Before opening a console session, let us define a Person model with name and age columns. rails generate model Person name:string age: integer invoke active_record create db/migrate/20250227112053_create_people.rb create app/models/person.rb invoke test_unit create test/models/person_test.rb create test/fixtures/people.yml This will create Person class derived…
-
Directory Structure
When you use the Rails helper script to create your application, it creates the entire directory structure for the application. Rails knows where to find things it needs within this structure, so you don’t have to provide any input. Shown below is a top-level view of a directory tree created by the helper script at…