Bruno Sutic
Rails & Javascript developer
Ideal Project Group, Chicago
github.com/bruno-
@brunosutic
Slide 3
Slide 3 text
What is Arel?
• github.com/rails/arel
• “Simplifies generation of complex SQL”
You write Ruby, out comes the SQL
• dependency of ActiveRecord
• can be used together with ActiveRecord
Slide 4
Slide 4 text
How I learned about Arel?
• credit: Janko Marohnic
• used Arel to optimize a feature on a project
Slide 5
Slide 5 text
Demo to get you interested :)
Slide 6
Slide 6 text
Prerequisites for the
examples
• ‘Customer’ is an ActiveRecord model
• Arel api for a column:
Customer.arel_table[:name]
• A “convention”:
class Customer
def self.[](column)
arel_table[column]
end
end
Customer[:name]
Slide 7
Slide 7 text
Basic example
• ActiveRecord
Customer.where(name: variable)
• Arel
Customer.where(Customer[:name].eq(variable))
Slide 8
Slide 8 text
A bit more useful example
• ActiveRecord
Customer.joins(:sales)
.where(“sales.price” => variable)
or
Customer.joins(:sales)
.where(sales: { price: variable })
• Arel
Customer.joins(:sales)
.where(Sale[:price].eq(variable))