What is Arel in Rails 3.0?

I understand that this is a replacement for ActiveRecord and that it uses objects instead of queries.

But...

why is it better?

Will objects / queries be "easier" to create?

will result in more efficient SQL queries?

Is it compatible with all major databases? - I guess it will be.

will it be easier / harder to use stored procedures?

+83
ruby ruby-on-rails activerecord arel
May 05, '10 at 3:28
source share
4 answers

What is Arel in Rails 3.0?

This is an object model for the algebra of relational query operators.

I understand that this is a replacement for ActiveRecord

No, it is not. This is a replacement for manual SQL queries in rows. This is the usual query level that underlies ActiveRecord, but it can also be used, for example, to support DataMapper.

If it's a replacement for anything, it's a replacement for Ambition. Or you can think of it as a Ruby version of the standard LINQ or Python SQLAlchemy queries. (In fact, the author explicitly refers to LINQ and SQLAlchemy as inspiration.)

Or you can see it as a replacement for named_scope s. In fact, AREL pretty much represents the idea that "every request is named_scope ". And, whaddayaknow: both were written by the same guy.

and that it uses objects instead of queries.

No, it uses objects as queries.

why is it better?

Ruby is an object oriented language, not a string language. For this reason, it makes sense to represent queries as objects instead of strings. Creating the right object model for queries instead of using strings for everything gives you pretty much the same advantages as creating the right object model for an accounting system, and not using strings for everything gives you.

Another big advantage is that AREL implements a real algebra of query operators. In other words, AREL is aware of the mathematical rules for constructing and compiling queries. If you concatenate two rows, each of which contains a valid SQL query, the result will probably not be a valid SQL query. Or, even worse, this is a valid SQL query, but one that doesn't make sense, or something completely different from what you think. This will never happen with AREL. (This is what I refer to below, meaning "closed in composition.")

Will objects / queries be "easier" to create?

Yes. For example, as I mentioned above, it is much easier to create more complex queries from simpler parts.

will result in more efficient SQL queries?

Yes. The fact that AREL has the proper object model for queries means that it can perform optimizations on those queries long before it ever generates an actual SQL query.

Is it compatible with all major databases? - I guess it will be.

Yes. In fact, I always talked about SQL above, but in fact, relational query algebra can generate queries for just about anything. Again, see LINQ or Ambition for examples: both can query SQL, LDAP, ActiveResource, CouchDB, Amazon, Google, & hellip; all with the same syntax.

Perhaps the best discussion of what AREL is and why Nick Kallen wrote is a well-titled article Why Arel? Nick Cullen . Note. The article contains a small mild mathematical and informational jargon, but itโ€™s exactly the same: AREL has some solid foundations in mathematics and computer science, these foundations are what gives it its powerful properties.

+179
May 05 '10 at 4:35 a.m.
source share

AREL, unfortunately, is directly related to SQL generation and, therefore, is not suitable for the needs of DataMapper.

As I would say, AREL is an explicit query model for ActiveRecord that generates and optimizes SQL queries for RDBMS.

DataMapper, on the other hand, is a genuine data mapper and can already interact with non-relational data stores. In the future, DataMapper will likely include a separate Veritas library that is designed to provide relational functionality for data retrieved from ANY data warehouse, not just RDBMS.

+19
May 6 '10 at 8:26
source share

Arel in Rails 3 creates relationship objects where db is not requested until you need it. Much more effective.

It is also more natural (as soon as you get used to it), which is really a great strength of Rails.

+1
Jul 23 '10 at 4:10
source share

In fact, I started a series of ActiveRelation videos.

The first general tutorial can be viewed at http://Innovative-Studios.com/#pilot

0
May 08 '10 at 17:07
source share



All Articles