Davis W. FrankDavis W. Frank
Standup 2010.06.14: as_if Edition
edit Posted by Davis W. Frank on Monday June 14, 2010 at 06:26PM

Interesting Things

If you are overriding to_json in any of your Rails models you should read Jonathan Julian's blog post which explains the purpose of as_json vs to_json.

Choice quote:

Enter ActiveSupport 2.3.3. Now the creation of the json is separate from therendering of the json. as_json is used to create the structure of the JSON as a Hash, and the rendering of that hash into a JSON string is left up to ActiveSupport::json.encode. You should never use to_json to create a representation, only to consume the representation.

as_json is also easier to test because it returns a Ruby Hash instead of a String.