Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Introduction to Arel
Search
Bruno Sutic
October 28, 2014
Programming
300
1
Share
Introduction to Arel
Arel fundamentals
Bruno Sutic
October 28, 2014
More Decks by Bruno Sutic
See All by Bruno Sutic
Readline + irb/pry = <3
brunosutic
0
150
Moreutils
brunosutic
0
73
The venerable "expect"
brunosutic
0
74
Capistrano vs Mina: Capistrano demo talk
brunosutic
0
470
Configuring tmux
brunosutic
0
110
Tmux osnove
brunosutic
0
220
Deploying Rails apps with Capistrano
brunosutic
0
130
Other Decks in Programming
See All in Programming
なぜあなたのコードには「コシ」がないのか?〜AI時代に問う、最後まで美味しい設計と戦略〜 #phpconkagawa / phpconkagawa2026
shogogg
0
110
Surviving Black Friday: 329 billion requests with Falcon!
ioquatix
0
2.5k
AIベース静的検査器の偽陽性率を抑える工夫3選
orgachem
PRO
4
390
いつか誰かが、と思っていた フロントエンド刷新5年間の実践知
kiichisugihara
1
250
PHPでバイナリをパースして理解するASN.1
muno92
PRO
0
380
Firefoxにコントリビューションして得られた学び
ken7253
2
150
JOAI2026 1st solution - heron0519 -
heron0519
0
170
AI-DLC Deep Dive
yuukiyo
9
5.3k
ソフトウェア設計の結合バランス #phperkaigi
kajitack
0
170
ついに来た!本格的なマルチクラウド時代の Google Cloud
maroon1st
0
340
20年以上続くプロダクトでも使い続けられる静的解析ツールを求めて
matsuo_atsushi
0
120
ふりがな Deep Dive try! Swift Tokyo 2026
watura
0
270
Featured
See All Featured
Navigating the Design Leadership Dip - Product Design Week Design Leaders+ Conference 2024
apolaine
0
300
State of Search Keynote: SEO is Dead Long Live SEO
ryanjones
0
190
Balancing Empowerment & Direction
lara
6
1.1k
Bootstrapping a Software Product
garrettdimon
PRO
307
120k
Building a Modern Day E-commerce SEO Strategy
aleyda
45
9k
Designing for Performance
lara
611
70k
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
360
30k
How to audit for AI Accessibility on your Front & Back End
davetheseo
0
350
How to make the Groovebox
asonas
2
2.1k
Connecting the Dots Between Site Speed, User Experience & Your Business [WebExpo 2025]
tammyeverts
11
900
Exploring anti-patterns in Rails
aemeredith
3
350
Building Flexible Design Systems
yeseniaperezcruz
330
40k
Transcript
Introduction to Arel
Bruno Sutic Rails & Javascript developer Ideal Project Group,
Chicago github.com/bruno- @brunosutic
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
How I learned about Arel? • credit: Janko Marohnic •
used Arel to optimize a feature on a project
Demo to get you interested :)
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]
Basic example • ActiveRecord Customer.where(name: variable) • Arel Customer.where(Customer[:name].eq(variable))
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))
SQL comparisons example • ActiveRecord Customer.where(“credits > ?”, credits_variable) •
Arel Customer.where( Customer[:credits].gt(credits_variable) ) • All comparison operators: gt, lt, gteq, lteq
OR condition example • ActiveRecord Customer.where(“credits > ? OR credits
< ?”, var1, var2) • Arel Customer.where( Customer[:credits].gt(var1).or( Customer[:credits].lt(var2) ) )
Complex conditions example • ActiveRecord Customer.where( “credits > ? OR
(name = ? AND credits < ?)”, var1, var2, var3 ) • Arel Customer.where( Customer[:credits].gt(var1).or( Customer[:name].eq(var2).and( Customer[:credits].lt(var3) ) ) )
LIKE example • ActiveRecord Customer.where(“name LIKE ?”, “%#{variable}%”) • Arel
Customer.where(Customer[:name].matches(“%#{variable}%”))
Other features • support for all SQL ‘join’ types •
SQL literals • writing only a part of a bigger query as SQL • example: CASE statement
Benefits of using Arel • pure Ruby, nicer looking code
• proper code highlighting • ruby exceptions • completely avoids SQL injection
Arel resources • github repo: https://github.com/rails/arel • great speech: https://www.youtube.com/watch?v=ShPAxNcLm3o
• source code walkthrough: https://www.youtube.com/watch?v=EJ6b_2S9Ids • examples from this talk: https://gist.github.com/bruno-/5964403476c791331c49
Questions?