Arelcheatsheet
贡献者:BAI
表(Table)
RBusers = Arel::Table.new(:users)users = User.arel_table # ActiveRecord model
字段(Field)
RBusers[:name]users[:id]
where
RBusers.where(users[:name].eq('amy'))# SELECT * FROM users WHERE users.name = 'amy'
select
(project)
RBusers.project(users[:id])# SELECT users.id FROM users
join
基本 join
在 ActiveRecord (非 Arel) 中, 如果 :photos
是关系的名字的话,使用 joins
RBusers.joins(:photos)
在 Arel 中, 需要将 photos
定义为 Arel 表
RBphotos = Photo.arel_tableusers.join(photos)users.join(photos, Arel::Nodes::OuterJoin).on(users[:id].eq(photos[:user_id]))
条件 Join
RBusers.joins(:photos).merge(Photo.where(published: true))
复杂些可以结合更多的 SQL 语句
RBusers.join( users.join(photos, Arel::Nodes::OuterJoin) .on(photos[:user_id].eq(users[:id]).and(photos[:published].eq(true))))
高级 Join
多个 joins
对同一张表但是表示不同的含义
RBcreators = User.arel_table.alias('creators')updaters = User.arel_table.alias('updaters')photos = Photo.arel_tablephotos_with_credits = photos.join(photos.join(creators, Arel::Nodes::OuterJoin).on(photos[:created_by_id].eq(creators[:id]))).join(photos.join(updaters, Arel::Nodes::OuterJoin).on(photos[:assigned_id].eq(updaters[:id]))).project(photos[:name], photos[:created_at], creators[:name].as('creator'), updaters[:name].as('editor'))photos_with_credits.to_sql# => "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`)"# after the request is done, you can use the attributes you named# it's as if every Photo record you got has "creator" and "editor" fields, containing creator name and editor namephotos_with_credits.map{|x| "#{photo.name} - copyright #{photo.created_at.year} #{photo.creator}, edited by #{photo.editor}"}.join('; ')
limit
/ offset
RBusers.take(5) # => SELECT * FROM users LIMIT 5users.skip(4) # => SELECT * FROM users OFFSET 4
Aggregates
RBusers.project(users[:age].sum) # .average .minimum .maximumusers.project(users[:id].count)users.project(users[:id].count.as('user_count'))
order
RBusers.order(users[:name])users.order(users[:name], users[:age].desc)users.reorder(users[:age])
和 ActiveRecord
RBUser.arel_tableUser.where(id: 1).arel
用 Arel 清理代码
RBphotos_with_credits = Photo.with_creator.with_editor
RBall_time = photos_with_credits.countthis_month = photos_with_credits.where(photos[:created_at].gteq(Date.today.beginning_of_month))recent_photos = photos_with_credits.where(photos[:created_at].gteq(Date.today.beginning_of_month)).limit(5)