The to_json serializer uses the method name as the key for serialization. Therefore, you cannot use the methods parameter to do this. Unfortunately, the to_json method to_json not have the t accept block` parameter, otherwise you could do something similar to
@obj.to_json(:except => :id) {|json| json.id = base62_id }
So this leaves us with an ugly hack, for example:
def to_json(options={}) oid, self.id = self.id, self.base62_id(self.id) super ensure self.id = oid end
Now to_json will return the expected result.
Harish shetty
source share