Close
Glad You're Ready. Let's Get Started!

Let us know how we can contact you.

Thank you!

We'll respond shortly.

PIVOTAL LABS
Standup 2010.06.14: as_if Edition

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.

Comments
Post a Comment

Your Information (Name required. Email address will not be displayed with comment.)

* Copy This Password *

* Type Or Paste Password Here *