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－
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.
This process is generally straight forward if you follow the following and write Rails-friendly SQL.
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
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 －
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 －
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.
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 －
Open book.rb in the appmodel subdiractory and put the following validations −
Check Rails quick Guidefor more details.
Ruby on Rails Related Interview Questions
|Python Interview Questions||HTML 5 Interview Questions|
|PHP Interview Questions||PostgreSQL Interview Questions|
|Ruby Interview Questions||Django Interview Questions|
|wxPython Interview Questions||MongoDB Interview Questions|
|Python Automation Testing Interview Questions||GitHub Interview Questions|
|Ruby Sinatra Interview Questions|
Ruby On Rails Tutorial
Ruby On Rails
All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd
Wisdomjobs.com is one of the best job search sites in India.