Adds the :only, :except, :methods, and :include options to AR#to_json. Oh, and AR#to_json won't return you "attributes" as part of the JSON - i.e. no more
{attributes: {id: 1, name: "foo"}
The 'attributes' part is often unnecessary cruft.
You get this instead:
{id: 1, name: "foo"}
Example uses are in the patch (check out the tests). It's time to treat the JSON encoding of AR objects with more respect ;). I'm tired of coding custom to_json methods or using the JSON gem to get any flexibility in respond_to blocks that wants.json.
Essentially you will be able to stuff like:
david.to_json(:include => { :posts => { :only => :title } })
The way it works is by converting AR objects into simple hashes that can be easily encoded as JSON.
Some things I'm not entirely happy with:
BTW, I do realize that the JSON here is not strictly valid (keys are not double quoted) - I'm just writing it the way Rails produces JSON by default. To get strictly valid JSON, add
ActiveSupport::JSON.unquote_hash_key_identifiers = false
in your environment.rb (or in the Rails initializers directory if you're on edge).