# Passing :methods to to\_json...sometimes

**URL:** <https://www.ruby-forum.com/t/passing-methods-to-to-json-sometimes/175087>\
**Category:** Rails\
**Created:** [September 7, 2009, 6:59pm UTC](https://www.ruby-forum.com/t/passing-methods-to-to-json-sometimes/175087 "2009-09-07T18:59:44Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![kelleysislander](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f6c823/32.png) [@kelleysislander](https://www.ruby-forum.com/u/kelleysislander)\
**Post date:** [September 7, 2009, 6:59pm UTC](https://www.ruby-forum.com/t/passing-methods-to-to-json-sometimes/175087/1 "2009-09-07T18:59:44Z")

</div>

In my application I have a class, Group, which has\_many  
GroupMemberships, and has\_many GroupMembers  
through :group\_memberships. My UI is almost purely JS, so my actions  
render everything in JSON, which I accomplish through a generic  
“build\_response” method that takes the data to be rendered, adds some  
elements to a hash, and eventually to\_json is called on the whole  
thing (by virtue of the fact that I’m calling render :json).

For most of my “views” showing a collection of Groups, I’m not  
interested in the associated GroupMembers. But there’s one action in  
one controller where I need to display them (/groups/show). I can  
easily show GroupMembers in to\_json thusly:

def to\_json(options = {})  
super({:methods =\> [:group\_members]}.merge(options))  
end

…but this causes a somewhat expensive database query that isn’t  
needed in most cases. The trick is figuring out how to cause to\_json  
to include the :group\_members association only in the case I need it  
to. If models could see controller instance variables, it’d be a no-  
brainer - but they can’t. It would also be easy if I were calling  
to\_json directly and could then pass something in the options param,  
but I never do. to\_json is always called on my behalf because my  
collection of Groups is a member of a larger hash which is being  
rendered as json by a controller action.

I’m stuck. Anyone have any ideas (preferably ones that don’t require  
me to re-architect my model of using a generic method to construct my  
data for JSON rendering)?

Thanks…

---

<div class="post-metadata">

**Author:** ![kelleysislander](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f6c823/32.png) [@kelleysislander](https://www.ruby-forum.com/u/kelleysislander)\
**Post date:** [September 9, 2009, 5:24am UTC](https://www.ruby-forum.com/t/passing-methods-to-to-json-sometimes/175087/2 "2009-09-09T05:24:15Z")

</div>

My solution:

class Group \< ActiveRecord::Base  
…  
def to\_json(options = {})  
methods = [:somethings] # Always include somethings  
methods \<\< :group\_members if options[:include\_group\_members]  
super({:methods =\> methods}.merge(options))  
end  
end

…and in the one action in the single controller where I want to show  
GroupMembers, I call to\_json directly on the larger hash that contains  
my array of Groups (among other things): render :json =\>  
that\_hash.to\_json(:include\_group\_members =\> true)

It turns out that when to\_json recurses into nested elements, it  
passes the options hash down the chain, so in my case it trickles down  
to my Group#to\_json method.

> > class Foo  
> > def to\_json(options = {})  
> > puts “f was #{options[:f]}”  
> > end  
> > end  
> > =\> nil
> 
> > {:foo =\> Foo.new}.to\_json(:f =\> ‘biff’)  
> > f was biff  
> > =\> “{“f1”:}”
