Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is ORM as related to Ruby on Rails?

What is ORM as it applies to Rails and what does it mean?

like image 523
Arun Avatar asked Feb 03 '10 19:02

Arun


3 Answers

ORM is Object Relational Mapper. It means you don't have to manually call the database yourself; the ORM handles it for you.

Ruby on Rails uses one called ActiveRecord, and it's a really good one.

ORM allows you to do things such as:

User.find(50).contacts

Instead of manually writing a SELECT statement with JOINs, WHEREs, etc.

like image 152
Mike Trpcic Avatar answered Sep 26 '22 23:09

Mike Trpcic


ORM stands for Object-Relational-Mapping. It basically means that Active Record takes data which is stored in a database table using rows and columns, which needs to be modified or retrieved by writing SQL statements (if you're using a SQL database), and it lets you interact with that data as though it was a normal Ruby object.

Example: Suppose you want to fetch an array of all the users then instead of writing any code for database connection and then writing some SQL query like SELECT * FROM users and converting the result into an array, I can just type User.all and Active Record gives me that array filled with User objects that I can play with as I'd like.

It doesn't really matter which type of database you're using. Active Record smooths out all the differences between those databases for you so you don't have to think about it. You focus on writing code for your application, and Active Record will think about the nitty gritty details of connecting you to your database. It also means that if you switch from one database to another, you don't actually need to change any major application code, just some configuration files.

like image 35
Rupali Avatar answered Sep 25 '22 23:09

Rupali


ORM is Object Relational Mapper. It means you don't have to manually call the database yourself; the ORM handles it for you. Ruby on Rails uses one called ActiveRecord, and it's a really good one.

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.
> Perform database operations in an object-oriented fashion.

click hear

like image 24
Anjan Avatar answered Sep 26 '22 23:09

Anjan