开发者

Rails 3 respond_with json question

Having trouble with generating some json. I am trying to render an single active record result to json like this:

@data = User.find(1)
respond_with(@data, :include => :status)

The json result is:

{
  -user: {
    address: null
    email: "test@email.com"
    first_name: "Test"
    last_name: "Man"
    status_id: 1
    username: "testguy"
    status: { }
  }
}

So whats the problem? The problem is that the :include=>:status seems to not bring over the relation. In my User model I have a belongs_to :status. How do i get this to work on a single result set?

When I do this:

@data = User.where("id = 1")
respond_with(@data, :include => :status)

The relation shows in the json result set fine this way. But its within an array of objects, which i 开发者_开发百科do not want.

Any ideas?


I assume that the status you want to include is not one of the http status codes (200, 201, etc.) nor an object associated with the User object in any way and is your own variable.

Using Rails 3.0.10 and Ruby 1.9.2 you may have find one of these two solutions suitable for you. Instead of respond_with use render as follows.

Solution 1

render :json => {:data => @data, :status => "whatever"}

The json result is:

{
  data:
  {
    user:
    {
      address: null
      email: "test@email.com"
      first_name: "Test"
      last_name: "Man"
      status_id: 1
      username: "testguy"
    }
  }
  status: "whatever"
}

Solution 2

render :json => {:user => {:address => @data.address, :email => @data.email, ..., :status => "whatever"}}

The json result is:

{
  user:
  {
    address: null
    email: "test@email.com"
    ...
    status: "whatever"
  }
}

I hope this is what you were looking for.


It sounds like you're having some of the same suffering I experienced with using the built in rails serialization. I ended up using RABL templates which made my life a lot easier. If you need a guide on getting up and running with RABL quickly, I've put one together:

http://blog.dcxn.com/2011/06/22/rails-json-templates-through-rabl/


where returns an array, so your results make sense. You'd need where().first, or since it's just an ID lookup, simply use find.

This was a long time ago, but still, this is the most concise answer:

@data = User.find(1)
respond_with(@data, :include => :status)


You might've solved it by now mate, but if you're still running into probs you can do it this way if you want?

@data = User.find(1, :include => :status)

respond_to do |format|
  format.json do
    render @data.to_json(:include => :status)
  end
end

If that doesn't work there may be something wrong with your associations. There's a really good section in the api on 'Eager loading of associations' here:

http://apidock.com/rails/ActiveRecord/Associations/ClassMethods


As nixterrimus pointed out RABL is really the way to go. It's simple, clean and elegant. I'm relatively new to rails and have not used these techniques (to_json, etc) but having done some research and actually using RABL, I cannot help but wonder why it's not a native feature of Rails.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜