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
Other ORM Tools for ruby
And there are some for Ruby on Rails (DrySQL), but Active Record is generally used with Rails
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.
What are some cons of using an ORM?
- If you are a master at SQL, you can probably get more performing queries by writing them yourself.
- There is overhead involved in learning how to use any given ORM.
- As a developer, it is important to understand what is happening under the hood. Since ORMs can serve as a crutch to avoid understanding databases and SQL, it can make you a weaker developer in that portion of the stack.
Some of the notable features of ActiveRecord are as follows:
- Data is represented as models
- Associations can be implemented using these models
- Inheritance can be achieved through related models
- Validation of data before being saved into the database
- Object oriented way of dealing with database operations
- Database Independent. This is probably the biggest advantageof using ORM in an application
- Domain Model Pattern
- Reduce Code and Increase Efficiency
- Rich Query Interface