simple web Engineering

November 27 2009

random Rails tips Vol. 1

Some random tips for your next Ruby on Rails project:

  • If you have to monkey patch gems or plugins (only monkey patch anything if you really have to, it can make finding errors a giant pain), make a Rails.root/lib/patches directory in which you keep all the (monkey) patches you need and include them in your environment.rb:

    This way you at least structure your monkey patching stuff better and know of the one place where strange behavior might originate.

  • Also if you monkey patch, don’t do any fancy (and often unneccessary) class_eval as long as it’s not possible to get the same result by simply reopening the class (or module) body. It’s by far more readable! Also see Yehuda Katz’s great blog post about this topic.
  • If you have to specify class names on associations that could not be automagically inferred from the associations’s name, always use the actual class and convert it to String instead of specifying a String directly.

    When you specify the class name as a String directly and change the class’s name, it will fail late, as opposed to when you convert the actual class to String will fail early (namely on startup) since you’ll be referencing a class that does not exist.

There are probably more random tips to come in the future so stay tuned.