97

Update:

This issue was not properly explored. The real issue lies within render :json.

The first code paste in the original question will yield the expected result. However, there is still a caveat. See this example:

render :json => current_user

is NOT the same as

render :json => current_user.to_json

That is, render :json will not automatically call the to_json method associated with the User object. In fact, if to_json is being overridden on the User model, render :json => @user will generate the ArgumentError described below.

summary

# works if User#to_json is not overridden
render :json => current_user

# If User#to_json is overridden, User requires explicit call
render :json => current_user.to_json

This all seems silly to me. This seems to be telling me that render is not actually calling Model#to_json when type :json is specified. Can someone explain what's really going on here?

Any genii that can help me with this can likely answer my other question: How to build a JSON response by combining @foo.to_json(options) and @bars.to_json(options) in Rails


Original Question:

I've seen some other examples on SO, but I none do what I'm looking for.

I'm trying:

class User < ActiveRecord::Base

  # this actually works! (see update summary above)
  def to_json
    super(:only => :username, :methods => [:foo, :bar])
  end

end

I'm getting ArgumentError: wrong number of arguments (1 for 0) in

/usr/lib/ruby/gems/1.9.1/gems/activesupport-2.3.5/lib/active_support/json/encoders/object.rb:4:in `to_json

Any ideas?

3
  • Your example works in one of my models. Do any of the username, foo or bar methods expect arguments? Commented Apr 3, 2010 at 19:07
  • No, username is not a method and foo and bar do not require methods. I updated my question to show where the error is happening.
    – maček
    Commented Apr 3, 2010 at 19:20
  • I'm running 1.8.7. You'll have to open up that file and see why it's passing an arg to a method that expects zero args. Commented Apr 3, 2010 at 19:21

4 Answers 4

226

You are getting ArgumentError: wrong number of arguments (1 for 0) because to_json needs to be overridden with one parameter, the options hash.

def to_json(options)
  ...
end

Longer explanation of to_json, as_json, and rendering:

In ActiveSupport 2.3.3, as_json was added to address issues like the one you have encountered. The creation of the json should be separate from the rendering of the json.

Now, anytime to_json is called on an object, as_json is invoked to create the data structure, and then that hash is encoded as a JSON string using ActiveSupport::json.encode. This happens for all types: object, numeric, date, string, etc (see the ActiveSupport code).

ActiveRecord objects behave the same way. There is a default as_json implementation that creates a hash that includes all the model's attributes. You should override as_json in your Model to create the JSON structure you want. as_json, just like the old to_json, takes an option hash where you can specify attributes and methods to include declaratively.

def as_json(options)
  # this example ignores the user's options
  super(:only => [:email, :handle])
end

In your controller, render :json => o can accept a string or an object. If it's a string, it's passed through as the response body, if it's an object, to_json is called, which triggers as_json as explained above.

So, as long as your models are properly represented with as_json overrides (or not), your controller code to display one model should look like this:

format.json { render :json => @user }

The moral of the story is: Avoid calling to_json directly, allow render to do that for you. If you need to tweak the JSON output, call as_json.

format.json { render :json => 
    @user.as_json(:only => [:username], :methods => [:avatar]) }
2
  • @Jonathan Julian, this is a very helpful explanation of as_json. As you can see in the ActiveRecord::Serialization docs (api.rubyonrails.org/classes/ActiveRecord/…), there is very little (no) documentation for this. I will give this a try :)
    – maček
    Commented Apr 4, 2010 at 17:53
  • 1
    @Jonathan Julian, if I could up-vote this 10 times, I would. Where the heck are the as_json docs! Thanks again :)
    – maček
    Commented Apr 4, 2010 at 18:07
71

If you're having issues with this in Rails 3, override serializable_hash instead of as_json. This will get your XML formatting for free too :)

4
  • 1
    Does anyone know of any good write-ups about the method serializable_hash? When I use it, it changes my subsequent xml output from wrapping the object with its name (e.g. "quote" for a quote object") to instead always wrap it with "<hash>". Commented Oct 20, 2011 at 0:17
  • @TylerCollier it should be same options as to_xml
    – Sam Soffes
    Commented Feb 23, 2012 at 19:03
  • Thanks for this solution! I'm using ruby2/rails4 and as_json wasn't working with nested objects, overridden method wasn't called in 'include', with serializable_hash it works!
    – santuxus
    Commented Aug 20, 2013 at 9:30
  • See robots.thoughtbot.com/better-serialization-less-as-json for an explanation of why serializable_hash should be overriden instead. Commented Aug 6, 2015 at 20:07
37

For people who don't want to ignore users options but also add their's:

def as_json(options)
  # this example DOES NOT ignore the user's options
  super({:only => [:email, :handle]}.merge(options))
end

Hope this helps anyone :)

1
  • 1
    This is the way I do it, except I default the options hash with = {} so it's not required when calling
    – mroach
    Commented Jul 18, 2018 at 6:08
5

Override not to_json, but as_json. And from as_json call what you want:

Try this:

def as_json 
 { :username => username, :foo => foo, :bar => bar }
end
6
  • Isn't as_json just for ActiveResource? Commented Apr 3, 2010 at 19:08
  • Apparently ActiveRecord::Serialization has as_json api.rubyonrails.org/classes/ActiveRecord/Serialization.html
    – glebm
    Commented Apr 3, 2010 at 19:12
  • @glebm, I tried this and I'm getting the same result. I updated my question to show you.
    – maček
    Commented Apr 3, 2010 at 19:22
  • @glebm, I'm still getting the exact same error. Even when I do render :json => current_user I get the expected default result (all attributes for User model in JSON format). When I add the as_json method to my User model and try the same thing, I get the error :(
    – maček
    Commented Apr 3, 2010 at 19:39
  • @glebm, thank you. I know I was doing something wrong. It might be worth checking out the updated question.
    – maček
    Commented Apr 3, 2010 at 20:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.