rails to_json

rails, including rails3, has this lovely bit of code related to providing a ‘to_json’ method on built-ins:

what this effectively does is clobber the ‘to_json’ method the json gem provides with one that active_support feels is better. the comment about json being broken is out of date and this was hack to begin with. in my case i really like this sort of thing in my /api controller: since it makes debugging a shit-ton easier. here’s how i did it in a rails3 app - step one was to make rails3 support the ‘preinitializer’ concept that old-skool rails used to have: next, i added this preinitializer: finally, this initializer: in english what this does is
  • make rails support code that runs before all else
  • during this phase suck in the json gem and remember how it does things
  • nuke the rails’ way and restore the json method of to_json