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
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
Bruno Sutic
October 28, 2014
Programming
1
280
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
62
The venerable "expect"
brunosutic
0
67
Capistrano vs Mina: Capistrano demo talk
brunosutic
0
460
Configuring tmux
brunosutic
0
100
Tmux osnove
brunosutic
0
200
Deploying Rails apps with Capistrano
brunosutic
0
120
Other Decks in Programming
See All in Programming
Claude Code の Skill で複雑な既存仕様をすっきり整理しよう
yuichirokato
1
270
20260228_JAWS_Beginner_Kansai
takuyay0ne
5
420
AI主導でFastAPIのWebサービスを作るときに 人間が構造化すべき境界線
okajun35
0
490
「ブロックテーマでは再現できない」は本当か?
inc2734
0
1.1k
今、アーキテクトとして 品質保証にどう関わるか
nealle
0
200
The Past, Present, and Future of Enterprise Java
ivargrimstad
0
350
要求定義・仕様記述・設計・検証の手引き - 理論から学ぶ明確で統一された成果物定義
orgachem
PRO
15
9.2k
猫の手も借りたい!ので AIエージェント猫を作って社内に放した話 Claude Code × Container Lambda の Slack Bot "DevNeko"
naramomi7
0
240
Oxlint JS plugins
kazupon
1
1.2k
AIプロダクト時代のQAエンジニアに求められること
imtnd
2
630
Takumiから考えるSecurity_Maturity_Model.pdf
gessy0129
0
110
The Past, Present, and Future of Enterprise Java
ivargrimstad
0
190
Featured
See All Featured
Paper Plane (Part 1)
katiecoart
PRO
0
5k
Agile Actions for Facilitating Distributed Teams - ADO2019
mkilby
0
140
HU Berlin: Industrial-Strength Natural Language Processing with spaCy and Prodigy
inesmontani
PRO
0
250
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
35
2.4k
Balancing Empowerment & Direction
lara
5
930
The Curious Case for Waylosing
cassininazir
0
260
A better future with KSS
kneath
240
18k
Hiding What from Whom? A Critical Review of the History of Programming languages for Music
tomoyanonymous
2
480
sira's awesome portfolio website redesign presentation
elsirapls
0
170
Become a Pro
speakerdeck
PRO
31
5.8k
Making Projects Easy
brettharned
120
6.6k
Collaborative Software Design: How to facilitate domain modelling decisions
baasie
0
150
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?