Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is Ruby on Rails ORM in layman's terms? Please explain

I am having trouble understanding ORM in Ruby on Rails. From what I understand there is a 1:1 relationship between tables/columns and objects/attributes. So every record is an object. Also what exactly is a Model? I know it maps to a table.

What I'm really after is a deeper understanding of the above. Thank you in advance for your help

I'm a Web developer going from PHP to Ruby on Rails.

like image 532
Imran Avatar asked Dec 17 '09 15:12

Imran


2 Answers

"From what I understand there is a 1:1 relationship between tables/columns and objects/attributes. So every record is an object."

That is not exactly correct, unless you use the term "object" very loosely. Tables are modelled by classes, while table records are modeled by instances of those classes.

Let's say you have a clients table, with columns id (autonum) and name (varchar). Let's say that it has only one record, id=1 and a name="Ford". Then:

  • The DB table clients will map to the model class Client.
  • The record will map to a model instance, meaning that you have to create the object and assign it to a variable in order to work with the record. The most common way would be to do ford = Client.find(1)
  • The two columns of the table will map to methods on the ford variable. You can do ford.id and you will get 1. You can do ford.name and you will get the string "Ford". You can also change the name of the client by doing ford.name = "Chevrolet", and then commit the changes on the database by doing ford.save.

"Also what exactly is a Model? I know it maps to a table"

Models are just classes with lots of very useful methods for manipulating your database. Here are some examples:

  • Validations: Besides the typical db-driven validations ("this field can't be null") you can implement much complex validations in ruby ("this field must be a valid email" is the most typical one). Validations are run just before you invoke "save" on a model instance.
  • Relationships: The foreign keys can also be mapped onto models. For example, if you had a brands table (with its corresponding Brand model) associated via a foreign key to your ford client, you could do ford.brands and you would get an array of objects representing all the records on the brands table that have a client_id = 1.
  • Queries: Models allow you to create queries in ruby, and translate them to SQL themselves. Most people like this feature.

These are just some examples. Active record provides much more functionalities such as translations, scoping in queries, or support for single table inheritance.

Last but not least, you can add your own methods to these classes.

Models are a great way of not writing "spaguetti code", since you are kind of forced to separate your code by functionality.

  • Models handle database interaction, and business logic
  • Views handle html rendering and user interaction
  • Controllers connect Models with Views
like image 88
kikito Avatar answered Oct 18 '22 00:10

kikito


ORM in Rails is an implementation of the Active Record pattern from Martin Fowler's Patterns of Enterprise Application Architecture book. Accordingly, the Rails ORM framework is named ActiveRecord.

The basic idea is that a database table is wrapped into a class and an instance of an object corresponds to a single row in that table. So creating a new instance adds a row to the table, updating the object updates the row etc. The wrapper class implements properties for each column in the table. In Rails' ActiveRecord, these properties are made available automatically using Ruby metaprogramming based on the database schema. You can override these properties if required if you need to introduce additional logic. You can also add so-called virtual attributes, which have no corresponding column in the underlying database table.

Rails is a Model-View-Controller (MVC) framework, so a Rails model is the M in MVC. As well as being the ActiveRecord wrapper class described above it contains business logic, including validation logic implemented by ActiveRecord's Validation module.

Further Reading

  • Rails Database Migrations guide
  • Rails Active Record Validations and Callbacks guide
  • Active Record Associations guide
  • Active Record Query Interface guide
  • Active Record API documentation
like image 20
John Topley Avatar answered Oct 18 '22 01:10

John Topley