Skip to content

Instantly share code, notes, and snippets.

@sondnm
Created December 1, 2017 15:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sondnm/6ffd0ede484c9947844a14f3898fb68c to your computer and use it in GitHub Desktop.
Save sondnm/6ffd0ede484c9947844a14f3898fb68c to your computer and use it in GitHub Desktop.
Arel cheatsheet

Tables

posts = Arel::Table.new(:posts)
posts = Post.arel_table # ActiveRecord

Columns

posts[:id]
posts[:title]

Select

posts.project(:id, :title) #=> "SELECT id, title FROM posts"
posts.project(posts[:id], posts[:title]) #=> "SELECT `posts`.`id`, `posts`.`title` FROM `posts`"
posts.project(posts[Arel.star]) #=> "SELECT `posts`.`*` FROM `posts`"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment