Living on the edge (of Rails) #7 - improved ActiveRecord::Base#attributes, time zone support

It’s that time of the week again (every Wednesday for the overly observant, coinciding with the release of the Rails Envy podcast) for some choice updates of what changes have been happening on edge Rails. This week’s report covers changes from 4 Feb 2008 to 10 Feb 2008 (the day the Rails Envy podcast was recorded).

More efficent ActiveRecord::Base#attributes

ActiveRecord::Base#attributes returns a hash of all attributes with clones of their objects as values. This unnecessary cloning is expensive, and has now been removed. ActiveRecord::Base#attributes now returns a hash of values of the attributes.

Thanks to Juanjo Bazan, there have been 2 further patches (after the podcast was recorded) related ActiveRecord::Base#attributes that further improves ActiveRecord’s performance: removal of cloning from ActiveRecord::Base#attributes_before_typecast, and removing options (:only and :except options, specifically) from ActiveRecord::Base#attributes.

Related changeset: http://dev.rubyonrails.org/changeset/8824

ActiveResouce::Base#exists? now uses HTTP HEAD instead of a GET

A HTTP HEAD request is really all that is needed to check for existence (200 == exists, 404 == doesn’t exist). This has been changed to HEAD from a GET request, which is more efficient since all we retrieve are the HTTP headers and there’s no unnecessary instantiation of the ActiveResource object.

Related changeset: http://dev.rubyonrails.org/changeset/8827

Time zone support for ActiveRecord, and config.time_zone property for specifying a default Time Zone

ActiveRecord models now have time zone-aware attribute readers and writers. Times returned from ActiveRecord attribute readers now have a time_zone attribute themselves, allowing time zone-aware calculations.

You can now configure the default Time.zone value with config.time_zone in your environment.rb. Setting this will enable time zone-awareness for ActiveRecord models.

Related changeset: http://dev.rubyonrails.org/changeset/8806

A Template class has been introduced to ActionView

As part of the ActionPack refactoring that’s still ongoing, a ActionView::Template class has been introduced to encapsulate all the data relevant to the currently rendered template. This is largely an internal refactoring so Rails users shouldn’t be affected.

Related changeset: http://dev.rubyonrails.org/changeset/8805

Concrete sweeper classes

You can now use an explicit class when declaring a cache_sweeper. E.g.

class ListsController < ApplicationController
  caches_action :index, :show, :public, :feed
  cache_sweeper Sweepers::List, :only => [ :edit, :destroy, :share ]
end

Related changeset: http://dev.rubyonrails.org/changeset/8819

As always, let me know of any inaccuracies or any suggestions you may have in the comments - this week’s report is a little rushed because I’m a little late in posting it up!

4 Comments & TrackBacks (Add yours)

The paper doll icon that precedes each comment is an idea conceived by Vanessa Tan.

Paper doll icon
sho's Gravatar

Found myself looking for this in my RSS reader today - didn’t specifically know it was every Wednesday, just seemed time and I looked forward to reading it! That should give you some idea of how valuable this series is.

Thanks a lot for keeping on top of this for us - I owe you a beer or three next time I’m in SG : )

One speculative question - do you have any idea of the timeframe for the next tagged release, hopefully including all this lovely new stuff? I’m sorry to say I have to run on a hacked version of Rails due to AR’s lack of native UUID support (you should know all about this, using Couch ..) and so edge rails isn’t really an option. Any clue?

Thanks again!

Posted by: sho on February 14, 2008 12am

Paper doll icon
Dr Nic's Gravatar

Perhaps encaspulating ActionView::Template will make it easier to reuse templates + render them from outside the request+controller stack. Currently it takes about 10 lines of code to mock out a request+controller etc.

Posted by: Dr Nic on February 14, 2008 5am

Paper doll icon
A Fresh Cup » Blog Archive » Double Shot #143's Gravatar

[…] Living on the edge (of Rails) #7 - improved ActiveRecord::Base#attributes, time zone support - More changes in edge Rails. […]

Posted by: A Fresh Cup » Blog Archive » Double Shot #143 on February 14, 2008 9pm

Paper doll icon
Chu Yeow's Gravatar

@sho: Thanks for the kind words! Regarding your question:

do you have any idea of the timeframe for the next tagged release, hopefully including all this lovely new stuff?

Judging from the recent major refactorings and some of the work still in progress, I think it’ll be quite some time before 2.1 gets released. This Ruby on Rails: Core thread has some opinions from Rails core committers.

There should be a 2.0.3 pretty soon but it’d only contain mostly bugfixes and not all the new stuff I’ve been blogging about.

@Dr Nic: I haven’t actually considered that (rendering the templates outside the request+controller stack) - good point!

Posted by: Chu Yeow on February 16, 2008 3pm

You can subscribe to the RSS feed for comments on this post.

Post a comment

(required)

(required, but never displayed)


You can format your comments using XHTML. Your email address will not be displayed or used for nefarious purposes.

Only following tags are allowed:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>