Aimred Developer Blog September 2007 Archive
Ruby Spin Talk Presentation Slides
We presented a talk on Ruby at the most recent SPIN (Software Process Improvement Network) meeting. The presentation (in PDF format) can be downloaded from their website.
Ambition: This Is Very Very Cool
Ambition is a very cool Ruby to SQL API (built on ParseTree, itself also very very cool) for ActiveRecord allowing you to generate and execute SQL using methods from the Enumerable mixin. Some examples:
User.first “SELECT * FROM users LIMIT 1" User.select { |m| m.name != ‘macgyver’ } “SELECT * FROM users WHERE users.`name` <> ’macgyver’" User.select { |u| u.email =~ /chris/ }.first “SELECT * FROM users WHERE (users.`email` REGEXP ‘chris’) LIMIT 1Sequel: Now With ParseTree Goodness
The latest 0.2 release of Sequel has incorporated some of those ParseTree ideas from Ambition so now you can do queries like the following (with the SQL that they generate underneath)
DB[:items].filter {:price < 100} # "SELECT * # FROM items # WHERE (price < 100)" DB[:items].filter {:category == 'Ruby'} # "SELECT * # FROM items # WHERE (category = 'Ruby')"
And to get more complex
DB[:orders].filter{ :price >= DB[:items].select(:price) } # "SELECT * # FROM orders # WHERE (price >= (SELECT price # FROM items))" DB[:items].filter do :x == 1 :y > 2 :z < 3 end # "SELECT * # FROM items # WHERE ((x = 1) # AND (y > 2) # AND (z < 3))"