This project is archived and is in readonly mode.

#4015 ✓invalid
Líus Fontenelle

Problem executing simple model#to_json

Reported by Líus Fontenelle | February 21st, 2010 @ 12:07 AM

I'm trying create and run a simple ("hello world") app on Rails 3 pre (and Ruby 1.9.1, under RVM). After installation succeed properly, I executed the follow command sequence:

$ rails ror3test $ cd ror3test $ rails generate scaffold user name:string $ rake db:migrate $ rails server

Until here, all it's ok. So, I added the following line on index action:

format.json { render :json => @users.to_json }

This is ok too, but if I call to_json method with any argument, like:

format.json { render :json => @users.to_json(:methods => :caps) }


format.json { render :json => @users.to_json(:only => [:name] }

the following error occur:

TypeError in UsersController#index
wrong argument type Hash (expected Data)

A more detailed error information is attached.

Comments and changes to this ticket

  • David Trasbo

    David Trasbo July 1st, 2010 @ 09:54 PM

    • Assigned user set to “Ryan Bigg”

    I was unable to duplicate this issue on master:

    ➜  ticket_4015 script/rails g model user name:string
    ➜  ticket_4015 rake db:migrate
    (in /Users/dtrasbo/code/rails/railties/tmp/ticket_4015)
    ➜  ticket_4015 script/rails g controller users index
    ➜  ticket_4015 script/rails c
    Loading development environment (Rails 3.0.0.beta4)
    ruby-1.9.2-preview3 > User.create(:name => 'David Trasbo')
     => #<User id: 1, name: "John Smith", ...> 
    ruby-1.9.2-preview3 > User.create(:name => 'Marc Hejner')
     => #<User id: 2, name: "David Trasbo", ...>
    class UsersController < ApplicationController
      def index
        @users = User.all
        respond_to do |format|
          format.json { render :json => @users.to_json(:only => :name) }

    Response body for http://localhost:3000/users.json:

    [{"user":{"name":"John Smith"}},{"user":{"name":"David Trasbo"}}]

    This ticket can be closed.

  • David Trasbo

    David Trasbo July 1st, 2010 @ 09:57 PM

    Gah, you'll probably notice I tried to change some names above, but looked over a couple of places.

  • David Trasbo

    David Trasbo September 9th, 2010 @ 01:22 PM

    • Assigned user changed from “Ryan Bigg” to “Rohit Arondekar”

    Closable, see above.

  • Rohit Arondekar

    Rohit Arondekar September 9th, 2010 @ 01:45 PM

    • State changed from “new” to “invalid”
    • Importance changed from “” to “Low”

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href=""></a>