# Serialize methods by to\_json

**URL:** <https://www.ruby-forum.com/t/serialize-methods-by-to-json/170836>\
**Category:** Rails\
**Created:** [June 23, 2009, 10:45am UTC](https://www.ruby-forum.com/t/serialize-methods-by-to-json/170836 "2009-06-23T10:45:42Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![gpierce](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/90ced4/32.png) [@gpierce](https://www.ruby-forum.com/u/gpierce)\
**Post date:** [June 23, 2009, 10:45am UTC](https://www.ruby-forum.com/t/serialize-methods-by-to-json/170836/1 "2009-06-23T10:45:42Z")

</div>

Hello,

Is there possible to pass arguments for serialized methods using  
to\_json??  
I want to serialize my collection of users with income and outcome which  
is calculate by methods:

collection.to\_json(:methods =\> [:income, :outcome])

But, what if I need to pass some arguments to method :outcome ?

Thanks

---

<div class="post-metadata">

**Author:** ![gpierce](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/90ced4/32.png) [@gpierce](https://www.ruby-forum.com/u/gpierce)\
**Post date:** [June 23, 2009, 4:41pm UTC](https://www.ruby-forum.com/t/serialize-methods-by-to-json/170836/2 "2009-06-23T16:41:54Z")

</div>

I don’t believe you can pass params using the methods option. One  
approach would be to calculate the outcome on the collection before  
calling to\_json.

def calculate\_outcome(arg)  
@outcome = # do calculation with arg  
end

def outcome  
@outcome  
end

collection.each {|c| c.calculate\_outcome(arg)}  
collection.to\_json(:methods =\> [:income, :outcome])

HTH,  
Nicholas

On Jun 23, 4:45 am, Gregory P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
