Skip to content

Rails 2.0 Upgrade Gotchas

In upgrading our product to use Rails 2.0.2, I ran into a number of upgrade issues. I’m documenting them here so that others might benefit.

ActiveRecord attribute methods

It seem that ActiveRecord attribute methods now being defined in method_missing. When method_missing is called, ActiveRecord will define methods for all attributes.  This works differently now than it did before.  In the past, I was able to override an attribute method dynamically.  Now, if I try to do this, it works fine at first, but once reset_column_information is called (which it is often in our system) you’ll see your method get overridden with a default version.

ActiveRecord does not appear to be checking to see if a method exists before overwriting it (it does check the parent classes, but not for dynamic meta-class methods).

To get around this, I overrode a core ActiveRecord::Base method and made sure to check that I wasn’t overriding methods that I need.

modele MyNewEval
  def evaluate_attribute_method(attr_name, method_definition, method_name=attr_name)
    if( self.column_config(attr_name).custom? && method_name['='] )
      # puts "ignoring redefinition of #{method_name}"
    elsif( self.column_config(attr_name).overridden? && !method_name['?'])
      # puts "ignoring redefinition of #{method_name}"
    else
      super
    end
  end
end
ActiveRecord::Base.extend( MyNewEval )

Seems that the Core ActiveRecord shouldn’t override methods that already exist, but that’s really just my opionion.  I’m sure there’s a better way to do this, but I’m not in a position at the moment to spend time figuring it out and this works for me.

Javascript UTF encoding

There are a number of tests where we’re checking javascript responses to make sure they contain certain information. In a few cases, we’re checking for htings that include strings like “somthing & something else”. The problem comes from the fact that in Rails 2.0, javascript strings are encoded a bit differently it seems. Now, we have to look for string like this “something \u0021amp; something else”.

RJS Hash keys quoted

This tripped up a number of our test cases.  What used to be:

{foo: "bar"}

Will now be generated as:

{"foo":"bar"}

@response.headers['Content-Type'] is now @response.headers['type']

Had some trouble getting a couple of tests working and this turned out to be the issue.  If you’re checking to be sure a response is coming back with a specific content type, headers['type'] seems to now contain that information.

Looks like there’s already a method @request.content_type, so you should probably be using that instead accessing the header directly anyway.

request.request_parameters => request.parameters

Looks like request.request_parameters now comes back as an empty hash({}) when it used to contain the controller/action information. Now you can get that from request.parameters instead.

Post a Comment

You must be logged in to post a comment.