using different key for to_json :methods

Posted by fphilipe on Stack Overflow See other posts from Stack Overflow or by fphilipe
Published on 2010-03-15T17:58:49Z Indexed on 2010/03/15 17:59 UTC
Read the original article Hit count: 240

Filed under:
|

When using :methods in to_json, is there a way to rename the key? I'm trying to replace the real id with a base62 version of it and I want that the value of base62_id has the key id.

@obj.to_json(
    :except => :id
    :methods => :base62_id
)

I tried to do

@obj.to_json(
    :except => :id
    :methods => { :id => :base62_id }
)

but that didn't work.

Any advice?

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about JSON