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
Sponsored
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
Bruno Sutic
October 28, 2014
Programming
1
270
Introduction to Arel
Arel fundamentals
Bruno Sutic
October 28, 2014
Tweet
Share
More Decks by Bruno Sutic
See All by Bruno Sutic
Readline + irb/pry = <3
brunosutic
0
140
Moreutils
brunosutic
0
59
The venerable "expect"
brunosutic
0
65
Capistrano vs Mina: Capistrano demo talk
brunosutic
0
460
Configuring tmux
brunosutic
0
100
Tmux osnove
brunosutic
0
190
Deploying Rails apps with Capistrano
brunosutic
0
110
Other Decks in Programming
See All in Programming
15年続くIoTサービスのSREエンジニアが挑む分散トレーシング導入
melonps
2
170
IFSによる形状設計/デモシーンの魅力 @ 慶應大学SFC
gam0022
1
300
今から始めるClaude Code超入門
448jp
7
8.5k
CSC307 Lecture 09
javiergs
PRO
1
830
生成AIを使ったコードレビューで定性的に品質カバー
chiilog
1
250
AIフル活用時代だからこそ学んでおきたい働き方の心得
shinoyu
0
130
コマンドとリード間の連携に対する脅威分析フレームワーク
pandayumi
1
450
AI時代の認知負荷との向き合い方
optfit
0
150
AIと一緒にレガシーに向き合ってみた
nyafunta9858
0
170
0→1 フロントエンド開発 Tips🚀 #レバテックMeetup
bengo4com
0
540
CSC307 Lecture 02
javiergs
PRO
1
770
Apache Iceberg V3 and migration to V3
tomtanaka
0
150
Featured
See All Featured
We Are The Robots
honzajavorek
0
160
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
508
140k
How Fast Is Fast Enough? [PerfNow 2025]
tammyeverts
3
450
Neural Spatial Audio Processing for Sound Field Analysis and Control
skoyamalab
0
160
The Anti-SEO Checklist Checklist. Pubcon Cyber Week
ryanjones
0
55
The AI Revolution Will Not Be Monopolized: How open-source beats economies of scale, even for LLMs
inesmontani
PRO
3
3k
Why Mistakes Are the Best Teachers: Turning Failure into a Pathway for Growth
auna
0
51
The Cult of Friendly URLs
andyhume
79
6.8k
Six Lessons from altMBA
skipperchong
29
4.1k
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
234
17k
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
12
1.4k
Ten Tips & Tricks for a 🌱 transition
stuffmc
0
64
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?