Skip to main content
Being a Rails developer we are familiar with ActiveRecord queries, but not so much with Arel. Arel is a library which allows us to customize our queries in an object-oriented manner without having to use manual SQL and string interpolation.

We often use where a method for querying the database. Where method can handle equality, null, arrays and now in Rails 4 it is also able to handle inequality condition with not method. But when there is a requirement of writing queries with OR statement or greater-than or less-than conditions most developers opt for writing SQL string queries. We have a better way to handle this situation using Arel.

For example:
If you want to select students having the date of birth greater than today’s date or date of birth less-than or equals to today’s date, usually a developer will write the query like this

Student.where("date_of_birth> ?", Date.today)
or
Student.where("date_of_birth<= ?", Date.today)

But this is not a good way to write such queries as we can write these queries in a more better way using Arel like this

Student.where(Student.arel_table[:date_of_birth].gt(Date.today))
or
Student.where(Student.arel_table[:date_of_birth].lteq(Date.today))

In the same way, ActiveRecord does not provide support for OR queries in earlier rails version.
If you want to fetch all the students having first as John or last name as Doe you can write the query like this but that is not the recommended way

Student.where(“first_name = ? OR last_name = ?”, “John”, “Doe”)

The recommended way of writing a query for the above scenario is

arel = Student.arel_table

Student.where(arel[:first_name].eq("John").or(arel[:last_name].eq("Doe")))

Same with the LIKE queries when we opt for writing SQL string literals

Arel has a keyword called matches for writing LIKE queries

Advantages of Using Arel are:

1. Follows DRY principle

Most of the time we write duplicate queries having one different parameter. We can avoid writing such queries using Arel. Because we can write queries in methods and that methods can be used for another query as well as we can design query builder using Arel.

2. Reliability

When we write Join query using SQL string it can break down due to ambiguity in column names but this is not the case with Arel.

3. Readability

Methods provided by Arel are readable to anyone who even don’t know much about coding. As we can write queries in parts using Arel it is much easier to understand in parts.

Arel allows rails developer to write queries in an object-oriented manner and it also allows us to write more complex queries with ease and modular code during software development using Ruby on Rails. Next time, when you stuck into complex queries go for Arel it will save time. This article just provides an overview of Arel as there is much more to explain in Arel.

Detail description is available in the official docs of Arel.

Comments

Popular posts from this blog

GraphQL With Ruby

Now a day’s most of the web or mobile applications fetch data from server which is stored in a database. REST API provides an interface to stored data that require by the applications. GraphQL is a query language for REST API's not for server databases. It is database agnostic and effectively can be used in any context where an API is used. GraphQL provide platform for declarative data fetching where client need to specify what data needs from API in response. Instead of multiple endpoints that return fixed data structures, a GraphQL server only exposes a single endpoint and responds with precisely the data a client asked for. GraphQL minimizes the amount of data that needs to be transferred over the network and improves applications operating under these conditions. Introduction to GraphQL API on Ruby on Rails Start with adding gem in Gemfile gem ‘graphql’ Run command bundle install Run command rails generate graphql:install Above command will add gr

Best In Place Gem In Ruby On Rails Tutorial

The best_in_place gem is the easiest solution for in place editing in Ruby on Rails. This gem provides functionality of “in place editing” in ruby on rails without writing any extra ajax code. It supports text inputs, textarea, select dropdown, checkboxes, jQuery UI Datepickers, etc. Also Displays server-side validation Installation Steps of “best_in_place” Gem : Installing best_in_place is very easy and straight-forward. Just begin including the gem in your Gemfile: gem ‘best_in_place’ After that, specify the use of the jquery and best in place javascripts in your application.js, and optionally specify jquery-ui if you want to use jQuery UI datepickers: //= require jquery //= require best_in_place //= require jquery-ui //= require best_in_place.jquery-ui Then, just add a binding to prepare all best in place fields when the document is ready: $(document).ready(function() { /* Activating Best In Place */ jQuery(".best_in_place").best_in_place(); });

Data scraping in Ruby on Rails using Nokogiri and Mechanize Gem

What is Data scraping? Website/Data  Scraping  is a technique to operating large amounts of  data  from websites whereby the  data  is extracted and displayed in own sites or it can be stored to a File/Database. Data scraping is basically used where the websites does not provides API. Some Applications do not provide API to collect records. For the same , Data Scraping technique is used. The data can be scraped using Nokogiri Gem. The steps are required: Add the gem “gem ‘nokogiri’, ‘~> 1.8’, ‘>= 1.8.1'” . Then run the bundle install Add the “require ‘nokogiri'” , “require ‘open-uri'” line where you will write the code for the scraping. The controller of the page will look like below: The view of the code of view page will look like : The result in our application will look like: Mechanize Gem in rails The Mechanize library is used for automating interaction with websites. Mechanize automatically stores and sends cookies, follo