Rails 1.2 RC1: New in Active Support
Posted by josh November 26, 2006 @ 04:33 PM
The following are some of the smaller, but notable features added to Rails 1.2 ActiveSupport since the Rails 1.1 release. (compiled by Joshua Sierles).
Module#unloadable marks constants that require unloading after each request. Example:
CONFIG.unloadable
Module#alias_attribute clones class attributes, including their getter, setter and query methods. Example:
class Email < ActiveRecord::Base
alias_attribute :subject, :title
end
e = Email.find(1)
e.title # => "Superstars"
e.subject # => "Superstars"
e.subject? # => true
e.subject = "Megastars"
e.title # => "Megastars"
Enumerable#sum calculates a sum from the array elements. Examples:
[1, 2, 3].sum
payments.sum { |p| p.price * p.tax_rate }
payments.sum(&:price)
This replaces: payments.inject(0) { |sum, p| sum + p.price }
Array#to_s(:db) produces a comma-separated list of ids. Example:
Purchase.find(:all, :conditions => "product_id IN (#{shops.products.to_s(:db)})"
Module#alias_method_chain encapsulates the common pattern:
alias_method :foo_without_feature, :foo
alias_method :foo, :foo_with_feature
With alias_method_chain:
alias_method_chain :foo, :feature
Array#split divides arrays into one or more subarrays by value or block. Examples:
[1, 2, 3, 4, 5].split(3) => [[1, 2], [4, 5]]
(1..10).to_a.split { |i| i % 3 == 0 } # => [[1, 2], [4, 5], [7, 8], [10]]
Hash.from_xml(string) creates a hash from an XML string, typecasting its elements if possible. Example:
Hash.from_xml <<-EOT
<note>
<title>This is a note</title>
<created-at type="date">2004-10-10</created-at>
</note>
EOT
...would return:
{ :note => { :title => "This is a note", :created_at => Date.new(2004, 10, 10) } }
The Builder package has been upgraded to version 2.0. Changes include:
-- UTF-8 characters in data are now correctly translated to their XML equivalents
-- Attribute values are now escaped by default

I’ve a question about the #sum method. A sum is a collection of numbers added together. But your example uses multiplication, which would be a product.
Are you saying we can pass a block to sum to override the default behavior of adding?
Also, what’s the purpose of…
payments.sum(&:price)
If I remember correctly, passing an argument prefixed by an ampersand is believed to be a Proc object…
So… I guess with no arguments it’s a standard sum calculation… with a block (or Proc) it overrides that default behavior. Is that about right?
Rabbit: The example sum the result of (price * tax_rate) for each record. The second example use the nifty Symbol#to_proc hack so &:price is the same as {|arg| arg.price}
What Tuxie said.
Basically, sum is just a clever encapsulation of Enumerable#map and Enumerable#inject. Blocks given to sum are run through map to get an array of Fixnums to send to inject. Pretty simple.
Quick question about alias attributes; will these work with validations just like a normal attribute?
I’ve gotta say that alias_attribute feels unDRY. Can someone explain why this might be useful and/or DRY?
Bill, I’m thinking API changes and such. But that’s guessing.
Sorry to put this here as it is off-topic, but… Can someone get the favicon working on this site? It’s difficult to pick out the feed in my reader sometimes.
I see… so the second example (with the block) returns to inject the singular value of price * tax as the value to be summed. Very cool. Thanks for the clarification guys. :)
I’m confused by the split method.. it looks like it’s actually dropping values out of the array while it splits it.. that doesn’t seem useful.
Why is this enumerable stuff in rails, it really doesn’t belong there.
@Bill I think that alias_ is usefull when you must work with pre esistent DB and you can’t change the old stupid column name.