Rails 2.3 upgrade gotcha: aggregate methods are now OrderedHash
Courtenay : May 8th, 2009
In our open source ticket tracker xtt, we have code that looks like this:
has_many :memberships do
def contexts
proxy_owner.memberships.sort.group_by &:context
end
end
Basically, it lets people group their memberships to various projects by a context. So each user can have their own grouping for projects. If you don't have a context for a project, it shows that last, in an implicit 'etc' context. In the tests, we check for this behavior like
it "leaves nil context for last" do
@contexts = @user.memberships.contexts
@contexts.last.should == [nil, [memberships(:default)]]
end
However, since rails now returns aggregated association results (such as group_by or total with :group) as an ActiveSupport::OrderedHash, you don't get all those tasty array methods that Rails is so famous for (#last, #second etc).
ActiveSupport::OrderedHash is just some code that duplicates Ruby 1.9's hash functionality.
The easy solution here is to make your results into an array at some point, rewrite your tests, or send a patch to the rails team to mix in the Array monkeypatched code into OrderedHash.
it "leaves nil context for last" do
@contexts = @user.memberships.contexts
@contexts.to_a.last.should == [nil, [memberships(:default)]]
end
1 Response to “Rails 2.3 upgrade gotcha: aggregate methods are now OrderedHash”
Sorry, comments are closed for this article.
May 9th, 2009 at 02:25 PM
Another gotcha I ran into was the removal of #sort! from OrderedHash