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

In: Edge Rails|Open Source|Ruby|Ruby on Rails

13 Feb 2008

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!

5 Responses to Living on the edge (of Rails) #7 – improved ActiveRecord::Base#attributes, time zone support

Avatar

sho

February 14th, 2008 at 12am

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!

Avatar

Dr Nic

February 14th, 2008 at 5am

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.

Avatar

A Fresh Cup » Blog Archive » Double Shot #143

February 14th, 2008 at 9pm

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

Avatar

Chu Yeow

February 16th, 2008 at 3pm

@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!

Avatar

puts Blog.new(”nonsense”) » Blog Archive » 5 Resources for Getting Up to Speed on Rails 2.1

July 8th, 2008 at 5pm

[…] #7 – improved ActiveRecord::Base#attributes, time zone support […]