Skip to content

Instantly share code, notes, and snippets.

@ernie
Created March 13, 2011 19:31
Show Gist options
  • Save ernie/868353 to your computer and use it in GitHub Desktop.
Save ernie/868353 to your computer and use it in GitHub Desktop.
Sample queries with MW 2.0
Article.joins{person.comments}.where{person.comments.body =~ '%hello%'}.to_sql
# => "SELECT \"articles\".* FROM \"articles\" INNER JOIN \"people\" ON \"people\".\"id\" = \"articles\".\"person_id\" INNER JOIN \"comments\" ON \"comments\".\"person_id\" = \"people\".\"id\" WHERE \"comments\".\"body\" LIKE '%hello%'"
Person.where{(id + 1) == 2}.first
# => #<Person id: 1, parent_id: nil, name: "Aric Smith", salary: 31000>
Person.where{(salary - 40000) < 0}.to_sql
# => "SELECT \"people\".* FROM \"people\" WHERE \"people\".\"salary\" - 40000 < 0"
p = Person.select{[id, name, salary, (salary + 1000).as('salary_after_increase')]}.first
# => #<Person id: 1, name: "Aric Smith", salary: 31000>
p.salary_after_increase # => 32000
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment