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\":}"

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to