December 31st, 2008 by Radar
Today fowlduck and I were talking in the #rubyonrails channel and we both wondered about timezones and why they were so (apparently) screwy. It turns out all I (and he possibly also) forgot to do was to put config.time_zone in the config/environment.rb. So what does this mysterious method do? Well:
- 1. The Configuration Class
This class begins all the way down on line #599 of railties/lib/initializer.rb in the Rails source. This just simply defines a class in the Rails module called Configuration. What we can get really excited about is on line #772 it defines an attr_accessor for :time_zone. This, as you probably already know defines two methods a setter (time_zone=) and a getter (time_zone) in which we can store values.
- 2. Your config/environment.rb file
By default this time_zone method will be set to nil. It’s up to you to set it in your config/environment.rb file which you do by doing something along these lines:
Rails::Initializer.run do |config|
config.time_zone = "Adelaide"
end
This will set the time_zone value to be the Adelaide Time zone, something like: #<ActiveSupport::TimeZone:0×30f4f8 @tzinfo=nil, @name=”Adelaide”, @utc_offset=34200>. You don’t have to set it to Adelaide, just try your nearest major city and it Should Just Work ™. If you don’t set this in your config/environment.rb date and time values returned from the database will not be set to whatever time zone you specify.
- 3. Back to you, Jeff.
When your application loads it processes the config/environment.rb file and runs the initialize_time_zone method which is defined further down.
This does all kinds of magic! Look at all the pretty sparkles! It firstly checks if you’ve set a time_zone in your config/environment.rb file and then if you have it sets the default time zone to be what you’ve specified. Additionally to this, it sets time_zone_aware_attributes to true so that when you do stuff like Topic.last.created_at it’ll actually return the time zoned version of that time. It does this by calling into play define_read_method_for_time_zone_conversion(attr_name) (click for juicy details) which either just returns the time or calls in_time_zone on the time returned which converts it into the time zone stored in Time.zone (which is actually Thread.current[:time_zone] if there is one stored there or otherwise the zone_default which was originally set when we called config.time_zone! What a mouthful!
By default, ActiveRecord will store timestamps as UTC as shown by ActiveRecord::Base.default_timezone = :utc. If you don’t specify a time zone in your config/environment.rb this value defaults to :local, so all times will be stored as the local time in your database.
- 4. And then…
So, assuming you did as the above when you go into your script/console and type: Topic.last.created_at you’ll get back the time when the topic was created relative to Adelaide. To change this, just redefine Time.zone by doing Time.zone= “Paris” and then doing Topic.last.created_at will give you time when the topic was created relative to Paris.
Posted in Uncategorized | No Comments »
December 30th, 2008 by Radar
Just leaked from Microsoft now:

Posted in Uncategorized | No Comments »
December 20th, 2008 by Radar
Today keiran_nz asked how to see SQL statements that occur when running code in script/console. After a little bit of mucking around in the terminal I worked it out to be:
tail -f log/development.log & script/console
The first comment on this post Ashley Williams says you could do:
tail -f log/development.log & clear && script/console
instead, and then goes further and sticks it in an alias in your .bashrc file (presumably):
alias rcon=”tail -f log/development.log & clear && script/console”
Posted in Uncategorized | 3 Comments »
December 14th, 2008 by Radar
exlibris asked tonight in #rubyonrails how to find the type of a column in the table for a model. I didn’t know this and maybe someone else out there may not know how either. The solution lies within the columns_hash method from ActiveRecord:Base which returns a hash containing all kinds of useful information about all the columns in your table such as:
- Precision (integer, notes the precision of the column if it’s decimal-based)
- Primary (boolean, identifies if column is primary key)
- default
- limit (integer, denotes how long the field can be)
- type (symbol, the class type in lowercase)
- name (string, fairly obvious)
- null (boolean, identifies if column can be set to null)
- scale (integer, does something)
- sql_type (returns the sql type of the column)
That final column is the magic attribute we’re looking for. To get to it we access it like Model.columns_hash["attribute"].sql_type
Tags: rails
Posted in Uncategorized | No Comments »
December 14th, 2008 by Radar
Sometime in the midst of January I’ll be moving up to Queensland to go work at Mocra with Bo Jeanes, Jack “Chendo” Chen, Dr Nic Williams and Claire Chapman.
This past week I had a work trial with them and really enjoyed my time up there; the people and code are just so fantastic. Friday was great fun with go-karting, the driving range and laser skirmish.
I’m looking forward to the fun of working there again in January and I do plan on having a going away party for my SA comrades sometime in January.
Posted in Uncategorized | 2 Comments »
December 2nd, 2008 by Radar
In this commit, this commit, this commit and this commit the Rails Core team shows that Engines in Rails are not as dead as once thought, and are making a comeback in (possibly) the next release which is either 2.3 or 3.0.
This is fantastic because when I’m asked if people can use rBoard they need to integrate it into an existing app, rather than having it stand alone. A lot of that would be massaging it all in to your app, but now you can have it as a separate plugin with it’s own app/* folders and config/routes.rb file too.
Posted in Uncategorized | No Comments »
December 1st, 2008 by Radar
Today, Google released possibly the most awesome invention since sliced bread, women and pants: Google Transit. This is something I would’ve loved to had when I was in school since Adelaide Metro’s website was so… ugh. This’ll also be useful when my car breaks down or comes across another pedestrian. Google has outdone themselves again and I think I’m in love with them (but not in the crazed stalker kind of way).
It would also appear that they need to work out some kinks.
Posted in Uncategorized | 2 Comments »
November 29th, 2008 by Radar
If you’re getting:
undefined method `cache_template_extensions=' for ActionView::Base:Class (NoMethodError)
You have a line in config/environments/development.rb and config/environments/production.rb that calls a deprecated method cache_template_extensions=. You need to remove this line in order to get your application to function properly. This is usually line #13 in both files.
Posted in Uncategorized | 2 Comments »
November 24th, 2008 by Radar
I’m going to be having a party at the Tap Inn starting @ 6:30pm @ The Tap Inn on the 4th December. Those wishing to come please contact me using the usual means.
Posted in Cool Shit, Uncategorized | No Comments »