diff options
author | Aaron Patterson <aaron.patterson@gmail.com> | 2011-03-07 08:30:09 -0800 |
---|---|---|
committer | Aaron Patterson <aaron.patterson@gmail.com> | 2011-03-07 08:30:09 -0800 |
commit | 8aa5d7a393e0bbef8cd3ae9ecf64c2358b802b5f (patch) | |
tree | 65bf0d13754591494117283ac9b8c076cd5799ea /README.markdown | |
parent | 2644bcec7dbe3a65277b3a6a141853484171535a (diff) | |
parent | 2158d592c074813471baa8fa20044b683bb156e6 (diff) | |
download | rails-8aa5d7a393e0bbef8cd3ae9ecf64c2358b802b5f.tar.gz rails-8aa5d7a393e0bbef8cd3ae9ecf64c2358b802b5f.tar.bz2 rails-8aa5d7a393e0bbef8cd3ae9ecf64c2358b802b5f.zip |
Merge remote branch 'stiff/master' into omg
* stiff/master:
implemented support for math operations in numeric attributes
Diffstat (limited to 'README.markdown')
-rw-r--r-- | README.markdown | 17 |
1 files changed, 17 insertions, 0 deletions
diff --git a/README.markdown b/README.markdown index b71879e4e3..4952ec6191 100644 --- a/README.markdown +++ b/README.markdown @@ -74,6 +74,23 @@ The `AND` operator behaves similarly. The examples above are fairly simple and other libraries match or come close to matching the expressiveness of Arel (e.g., `Sequel` in Ruby). +#### Inline math operations + +Suppose we have a table `products` with prices in different currencies. And we have a table currency_rates, of constantly changing currency rates. In Arel: + + products = Arel::Table.new(:products) + products.columns # => [products[:id], products[:name], products[:price], products[:currency_id]] + + currency_rates = Arel::Table.new(:currency_rates) + currency_rates.columns # => [currency_rates[:from_id], currency_rates[:to_id], currency_rates[:date], currency_rates[:rate]] + +Now, to order products by price in user preferred currency simply call: + + products. + join(:currency_rates).on(products[:currency_id].eq(currency_rates[:from_id])). + where(currency_rates[:to_id].eq(user_preferred_currency), currency_rates[:date].eq(Date.today)). + order(products[:price] * currency_rates[:rate]) + #### Complex Joins Where Arel really shines in its ability to handle complex joins and aggregations. As a first example, let's consider an "adjacency list", a tree represented in a table. Suppose we have a table `comments`, representing a threaded discussion: |