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
1
190
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
100
Moreutils
brunosutic
0
42
The venerable "expect"
brunosutic
0
52
Capistrano vs Mina: Capistrano demo talk
brunosutic
0
420
Configuring tmux
brunosutic
0
69
Tmux osnove
brunosutic
0
150
Deploying Rails apps with Capistrano
brunosutic
0
68
Other Decks in Programming
See All in Programming
良いユニットテストを書こう
mototakatsu
4
1.8k
Stackless и stackful? Корутины и асинхронность в Go
lamodatech
0
650
103 Early Hints
sugi_0000
1
220
フロントエンドのディレクトリ構成どうしてる? Feature-Sliced Design 導入体験談
osakatechlab
8
4.1k
テストコード文化を0から作り、変化し続けた組織
kazatohiei
2
1.5k
LLM Supervised Fine-tuningの理論と実践
datanalyticslabo
3
970
Full stack testing :: basic to basic
up1
1
930
これでLambdaが不要に?!Step FunctionsのJSONata対応について
iwatatomoya
2
3.6k
Cloudflare MCP ServerでClaude Desktop からWeb APIを構築
kutakutat
1
530
Асинхронность неизбежна: как мы проектировали сервис уведомлений
lamodatech
0
660
今からはじめるAndroidアプリ開発 2024 / DevFest 2024
star_zero
0
1k
複雑な仕様に立ち向かうアーキテクチャ
myohei
0
170
Featured
See All Featured
Typedesign – Prime Four
hannesfritz
40
2.4k
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
44
6.9k
Producing Creativity
orderedlist
PRO
341
39k
YesSQL, Process and Tooling at Scale
rocio
169
14k
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
26
1.9k
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
47
5.1k
Building a Modern Day E-commerce SEO Strategy
aleyda
38
7k
Building a Scalable Design System with Sketch
lauravandoore
460
33k
Put a Button on it: Removing Barriers to Going Fast.
kastner
59
3.6k
Raft: Consensus for Rubyists
vanstee
137
6.7k
Bash Introduction
62gerente
608
210k
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
251
21k
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?