Skip to content

Instantly share code, notes, and snippets.

@devhero
Last active August 29, 2015 14:24
Show Gist options
  • Save devhero/93c3968808b54ad5f870 to your computer and use it in GitHub Desktop.
Save devhero/93c3968808b54ad5f870 to your computer and use it in GitHub Desktop.
arel getting started
# equivalent constructors of arel table
model = Arel::Table.new 'model_table_name'
model = Arel::Table.new :model_table_name
model = Model.arel_table
# prepare arel query
model_query = model.project(model[Arel.star]).where(model[:id].lte(20))
# executing query and obtains an array of Model objects
model_list = Model.find_by_sql model_query
# executing query obtains an array of hashes
resultset = ActiveRecord::Base.connection.execute model_query
# references
http://blog.donwilson.net/2011/11/constructing-a-less-than-simple-query-with-rails-and-arel/
https://github.com/rails/arel
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment