10 Arel is an SQL abstraction library built into Ruby on Rails.
12 * <https://github.com/rails/arel>
17 users = Arel::Table.new(:users)
18 users = User.arel_table # ActiveRecord model
28 ### `where` (restriction)
31 users.where(users[:name].eq('amy'))
32 # SELECT * FROM users WHERE users.name = 'amy'
35 ### `select` (projection)
38 users.project(users[:id])
39 # SELECT users.id FROM users
44 In ActiveRecord (without Arel), if `:photos` is the name of the association, use `joins`
49 In Arel, if `photos` is defined as the Arel table,
51 photos = Photo.arel_table
53 users.join(photos, Arel::Nodes::OuterJoin).on(users[:id].eq(photos[:user_id]))
56 #### join with conditions
58 users.joins(:photos).merge(Photo.where(published: true))
61 If the simpler version doesn't help and you want to add more SQL statements to it:
64 users.join(photos, Arel::Nodes::OuterJoin)
65 .on(photos[:user_id].eq(users[:id]).and(photos[:published].eq(true)))
70 multiple `joins` with the same table but different meanings and/or conditions
72 creators = User.arel_table.alias('creators')
73 updaters = User.arel_table.alias('updaters')
74 photos = Photo.arel_table
76 photos_with_credits = photos
77 .join(photos.join(creators, Arel::Nodes::OuterJoin).on(photos[:created_by_id].eq(creators[:id])))
78 .join(photos.join(updaters, Arel::Nodes::OuterJoin).on(photos[:assigned_id].eq(updaters[:id])))
79 .project(photos[:name], photos[:created_at], creators[:name].as('creator'), updaters[:name].as('editor'))
81 photos_with_credits.to_sql
82 # => "SELECT `photos`.`name`, `photos`.`created_at`, `creators`.`name` AS creator, `updaters`.`name` AS editor FROM `photos` INNER JOIN (SELECT FROM `photos` LEFT OUTER JOIN `users` `creators` ON `photos`.`created_by_id` = `creators`.`id`) INNER JOIN (SELECT FROM `photos` LEFT OUTER JOIN `users` `updaters` ON `photos`.`updated_by_id` = `updaters`.`id`)"
84 # after the request is done, you can use the attributes you named
85 # it's as if every Photo record you got has "creator" and "editor" fields, containing creator name and editor name
86 photos_with_credits.map{|x|
87 "#{photo.name} - copyright #{photo.created_at.year} #{photo.creator}, edited by #{photo.editor}"
91 ### `limit` / `offset`
94 users.take(5) # => SELECT * FROM users LIMIT 5
95 users.skip(4) # => SELECT * FROM users OFFSET 4
101 users.project(users[:age].sum) # .average .minimum .maximum
102 users.project(users[:id].count)
103 users.project(users[:id].count.as('user_count'))
109 users.order(users[:name])
110 users.order(users[:name], users[:age].desc)
111 users.reorder(users[:age])
114 ### With ActiveRecord
118 User.where(id: 1).arel
121 ### Clean code with arel
123 Most of the clever stuff should be in scopes, e.g. the code above could become:
125 photos_with_credits = Photo.with_creator.with_editor
128 You can store requests in variables then add SQL segments:
130 all_time = photos_with_credits.count
131 this_month = photos_with_credits.where(photos[:created_at].gteq(Date.today.beginning_of_month))
132 recent_photos = photos_with_credits.where(photos[:created_at].gteq(Date.today.beginning_of_month)).limit(5)