Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the Difference between Active record and ORM in rails?

Is there any difference between Actice Record and ORM? some of the documentations says both are same. is that true?

like image 614
John Avatar asked Jul 25 '16 08:07

John


People also ask

What is ActiveRecord and ORM in rails?

1.3 Active Record as an ORM Framework Active Record gives us several mechanisms, the most important being the ability to: Represent models and their data. Represent associations between these models. Represent inheritance hierarchies through related models. Validate models before they get persisted to the database.

Is ActiveRecord an ORM?

ActiveRecord is an ORM. It's a layer of Ruby code that runs between your database and your logic code.

What is an ActiveRecord in rails?

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.

What ORM is used in Rails?

In Rails, ORM is implemented by Active Record which is one of the most important components of the Rails library. An ORM provides a mapping layer between how a database handles its data and how an object-oriented application handles its data.


2 Answers

Object Relational Mapping (ORM) is the technique of accessing a relational database using an object-oriented programming language. Object Relational Mapping is a way to manage database data by "mapping" database tables to classes and instances of classes to rows in those tables.

Active Record is just one of such ORMs, others include:

  • Sequel
  • DataMapper
  • Squeel
  • Ruby Object Mapper etc.

Read more here https://github.com/learn-co-students/active-record-mechanics-crud-v-000#orm-vs-active-record

like image 182
Joshua Azemoh Avatar answered Sep 21 '22 19:09

Joshua Azemoh


Object Relational Mapping (ORM):

simplify the use of databases in applications.

Use objects to hold database records

  1. One class for each table in the database
  2. Objects of the class correspond to rows in the table
  3. Attributes of an object correspond to columns from the row

    • Manage the movement of information between objects and the back-end database.
    • Manage relationships between tables (joins), turn into linked data structures.

ActiveRecord Basics

Model: A Rails class corresponding to a database table ActiveRecord:

  • Base class for models in Rails
  • Implements Object Relational Mapping

Example Table

<table>
  <tbody>
    <tr>
      <th> id </th>
      <th>name</th>
      <th>birth</th>
      <th>gpa</th>
      <th>grade</th>
    </tr>
  </tbody>
  <tbody>
    <tr>
      <td> 1 </td>
      <td> Anderson </td>
      <td> 1987-10-22 </td>
      <td>  3.9 </td>
      <td>  2009  </td>
    </tr>
     <tr>
      <td> 2 </td>
      <td> Jones </td>
      <td> 1990-04-16</td>
      <td> 2.4 </td>
      <td>  2012   </td>
    </tr>
 </tbody>
  
  </table>

Create a class for this table (app/models/student.rb):

class Student < ActiveRecord::Base
end

Or, just use the script/generate program:

ruby script/generate model student
  • ActiveRecord examines the database schema for this table and makes appropriate attributes and methods available in the class automatically
like image 25
Vishal Avatar answered Sep 21 '22 19:09

Vishal