Ruby on Rails - Ruby on Rails Active Record - ruby on rails tutorial - rails guides - rails tutorial - ruby rails



What is Active Record?

  • Data is mostly stored in relational database tables.
  • There is an essential mismatch between your program's object view and database's relational data view.
  • One way to resolve this mismatch was through the use of Object-relational-mapping (ORM) tools.
  • ORM is the mapping of relational database tables to object-oriented classes.
  • In Rails, ORM is implemented by Active Record which is one of the most important components of the Rails library.
  • Each Active Record object has CRUD (Create, Read, Update, and Delete) methods for database access.
 learn ruby on rails tutorial - active records - ruby on rails example

learn ruby on rails tutorial - active records - ruby on rails example

  • Its code works well even with less number of lines.
  • Active Record Rails application does not need any configuration at all, if proper naming schemes is followed in your database and classes.
  • There is one more feature of Active Record that makes your work easy, the implementation of a domain-specific-language (DSL).
  • A DSL is a programming language intended to use in a specific problem domain.

How to Translate a Domain Model into SQL?

  • Translating a domain model into SQL is generally straight forward, as long as you remember that you have to write Rails-friendly SQL. In practical terms, you have to follow certain rules −
    • Each entity (such as book) gets a table in the database named after it, but in the plural (books).
    • Each such entity-matching table has a field called id, which 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).

What are Active Record Basics?

  • Some of the basics of Active Record are classes, objects and naming conventions.

Active Record Classes and Objects

  • Each table in a database is generally represented by a class that extends Active Record base class. By extending Active Record base classes, model objects inherit a lot of functionalities.
  • While using Active Records, you don't have to set up any database connections. It manages all the database connections for an application. It adds attributes to your class for each of the columns in the database.

Active Record naming conventions

  • Active Record uses the CoC (convention over configuration) principle. On following naming convention, you can take advantage of many dynamic features of Active Record without any configuration.
ruby on rails tutorial tags - ruby , rail , ruby on rails , rail forum , ruby on rails tutorial , ruby tutorial , rails guides , rails tutorial , learn ruby

What are Class and Database?

  • Database table should be named in the plural form and in lowercase of your model classes.
  • Rails provides a facility where you can add plurals for a model.
  • To define your own pluralization, add code to the config/environment.rb using Inflector.
  • In some case, if you don't want to name your database in the plural form, Rails can be configured with singular-named database tables by adding following line to the config/environment.rb :
ActiveRecord::Base.pluralize_table_names = false  
Clicking "Copy Code" button will copy the code into the clipboard - memory. Please paste(Ctrl+V) it in your destination. The code will get pasted. Happy coding from Wikitechy - ruby on rails tutorial - rails guides - ruby rails - rubyonrails - learn ruby on rails - team

How to Create Active Record Files (Models)?

  • To create the Active Record files for our entities for library application, introduced in the previous chapter, issue the following command from the top level of the application directory.
library\> rails script/generate model Book
library\> rails script/generate model Subject
Clicking "Copy Code" button will copy the code into the clipboard - memory. Please paste(Ctrl+V) it in your destination. The code will get pasted. Happy coding from Wikitechy - ruby on rails tutorial - rails guides - ruby rails - rubyonrails - learn ruby on rails - team
  • Above rails generate model book commands generates the auto code as below −
 learn ruby on rails tutorial - rails generate model book - ruby on rails example

learn ruby on rails tutorial - rails generate model book - ruby on rails example

  • This is a Rails paradigm that you should follow each time you create a model.
  • Apart from creating many other files and directories, this will create files named book.rb and subject.rb containing a skeleton definition in the app/models directory.

Content available in book.rb −

class Book < ActiveRecord::Base
end
Clicking "Copy Code" button will copy the code into the clipboard - memory. Please paste(Ctrl+V) it in your destination. The code will get pasted. Happy coding from Wikitechy - ruby on rails tutorial - rails guides - ruby rails - rubyonrails - learn ruby on rails - team

Content available in subject.rb −

class Subject < ActiveRecord::Base
end
Clicking "Copy Code" button will copy the code into the clipboard - memory. Please paste(Ctrl+V) it in your destination. The code will get pasted. Happy coding from Wikitechy - ruby on rails tutorial - rails guides - ruby rails - rubyonrails - learn ruby on rails - team
 learn ruby on rails tutorial - active record - ruby on rails example

learn ruby on rails tutorial - active record - ruby on rails example


This ruby on rails tutorial page provides you the following key areas such as ruby , rail , ruby on rails , rail forum , ruby on rails tutorial , ruby tutorial , rails guides , rails tutorial , learn ruby , rails form_for , ruby rails , ruby class , what is ruby on rails , rails installer , ruby online , learn ruby on rails , ruby on rails jobs , rails find_by , install rails , easyrail , rubyonrails , link_to rails , ruby on rails developer , learn ruby the hard way , railscasts , ruby on rails examples , ruby on rails vs php , rails 4 , rails activerecord , rails generate , ruby and rails , ruby on rails download , install ruby on rails , ruby net http , what is rails , ruby app , ruby vs ruby on rails , ruby on rails windows , rails for zombies , ruby on rails book , ruby on rails development , ruby on rails ide , ruby on rails tutorial pdf

Related Searches to Ruby on Rails Active Record