Filter a model's attributes before outputting as json
Posted
by
Jorge Israel Peña
on Stack Overflow
See other posts from Stack Overflow
or by Jorge Israel Peña
Published on 2010-12-29T01:50:31Z
Indexed on
2010/12/29
1:53 UTC
Read the original article
Hit count: 709
Hey guys, I need to output my model as json and everything is going fine. However, some of the attributes need to be 'beautified' by filtering them through some helper methods, such as number_to_human_size
. How would I go about doing this?
In other words, say that I have an attribute named bytes
and I want to pass it through number_to_human_size
and have that result be output to json.
I would also like to 'trim' what gets output as json if that's possible, since I only need some of the attributes. Is this possible? Can someone please give me an example? I would really appreciate it.
© Stack Overflow or respective owner