Rails 2.3.4 and acts_as_favorite incompatibility

Usually, I try to write about information problems, and what we can do to better parse from the myriad of information. I have been involved in gigzee, which is built in Ruby on Rails, and like everyone else, we upgraded to Rails 2.3 a few weeks ago. We also use a customized version of the acts_as_favorite plugin internally to track which artists, gigs and venues people like. So, it was very disconcerting when after the upgrade the acts_as_favorite plugin stopped working for us. Worse, since user favorites is a central theme of our website, it pretty much brought down our entire system.

After lot of looking around the web, and poking around on our servers, we figured out the problem, and are posting our solution so that other people can find it useful. The problem, as we found, is that the acts_as_favorite overloads method_missing to extend the methods for the base class, say the User model. This way, it is able to provide new methods to that class, for example user.favorite_blogs.

Unfortunately, in rails 2.3.4, the file /Library/Ruby/Gems/1.8/gems/activerecord-2.3.4/lib/active_record/associations/association_proxy.rb got changed, to include the highlighted lines. These end up raising a NoMethodError before the overloaded method_missing is called for the @target.

def method_missing(method, *args)
  if load_target
    unless @target.respond_to?(method)
      message = "undefined method `#{method.to_s}' for \"#{@target}\":#{@target.class.to_s}"
      raise NoMethodError, message
    end

    if block_given?
      @target.send(method, *args)  { |*block_args| yield(*block_args) }
    else
      @target.send(method, *args)
    end
  end
end

Instead of messing with the gem itself, and breaking who-knows-what-else, we decided to simply overload the respond_to? method for the user class.Our code (in app/models/user.rb):

def respond_to?(method_sym)
  if method_sym.to_s =~ Regexp.new("^favorite_(\\w+)")
    return true
  elsif method_sym.to_s =~ Regexp.new("^old_favorite_(\\w+)")
    return true
  elsif method_sym.to_s =~ Regexp.new("^has_favorite_(\\w+)\\?")
    return true
  elsif method_sym.to_s =~ Regexp.new("^has_old_favorite_(\\w+)\\?")
    return true
  else
    super
  end
rescue
  super
end

This essentially goes around the problem by telling ActiveRecord that these methods do exist for the User model, and then the method_missing from the acts_as_favorite plugin is called.

Simple fix, and I hope it can save other people a lot of headache too.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s