Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
Introduction to Arel
Search
Bruno Sutic
October 28, 2014
Programming
360
1
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
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
190
Moreutils
brunosutic
0
92
The venerable "expect"
brunosutic
0
89
Capistrano vs Mina: Capistrano demo talk
brunosutic
0
490
Configuring tmux
brunosutic
0
130
Tmux osnove
brunosutic
0
260
Deploying Rails apps with Capistrano
brunosutic
0
160
Other Decks in Programming
See All in Programming
[Dev Group Paris Octobre 26] Agentforce, SIforce, Claudeforce, Slackbot… on s'y retrouve ?
nabondance
0
130
自作Discord音声botをLambda MicroVMsに移行した話 / Migrating My Discord Voice Bot to Lambda MicroVMs
akihisaikeda
1
250
조합이폭발하는디자인시스템이걸눈으로도테스트하라고요
giljongyu
0
700
느린 네트워크와 저사양 디바이스 사용자를 위한 웹뷰 로딩 시간 개선기
kmkim
0
140
AmebaブログはKMPから卒業します
kmars0605
0
310
flutter_test のその先へ — Flutter アプリの LLM 機能に「評価」という品質保証を
kosukesaigusa
0
180
Modernizing iOS Apps
seyfoyun
0
150
HTML-in-Canvasで作る未来のWeb UI表現
clockmaker
5
6.2k
React Native에 현대적인 빌드 도구 더하기: Rollipop 구현기
leegeunhyeok
0
180
作れる、でも消せるか?──安全に「枯らして消す」機能削除術 / Drain, Then Delete
chobishiba
3
1.4k
FactoryBotをやめればRSpecは速くなるのか?
yuta_onishi_97
0
1.5k
あなたの await、OS まで届いてる? strace で覗く JavaScript の非同期
hasutosasaki
1
1.8k
Featured
See All Featured
JAMstack: Web Apps at Ludicrous Speed - All Things Open 2022
reverentgeek
1
670
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
50
10k
VelocityConf: Rendering Performance Case Studies
addyosmani
331
25k
The AI Revolution Will Not Be Monopolized: How open-source beats economies of scale, even for LLMs
inesmontani
PRO
3
3.8k
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
162
16k
A Tale of Four Properties
chriscoyier
163
24k
Speed Design
sergeychernyshev
33
2.2k
Leading Effective Engineering Teams in the AI Era
addyosmani
9
2.8k
GitHub's CSS Performance
jonrohan
1033
470k
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
12
1.9k
Navigating Weather and Climate Data
rabernat
0
580
Reflections from 52 weeks, 52 projects
jeffersonlam
356
22k
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?