简体   繁体   English

Rails 3回答了json问题

[英]Rails 3 respond_with json question

Having trouble with generating some json. 生成一些json有困难。 I am trying to render an single active record result to json like this: 我试图将一个活动记录结果呈现给json,如下所示:

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

The json result is: json的结果是:

{
  -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. 问题是:include =>:status似乎没有带来关系。 In my User model I have a belongs_to :status. 在我的用户模型中,我有一个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. 这种关系在json结果集中表现得很好。 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. 我假设您要包含的状态不是http状态代码(200,201等)之一,也不是以任何方式与User对象关联的对象,而是您自己的变量。

Using Rails 3.0.10 and Ruby 1.9.2 you may have find one of these two solutions suitable for you. 使用Rails 3.0.10和Ruby 1.9.2,您可能会发现这两种解决方案中的一种适合您。 Instead of respond_with use render as follows. 而不是respond_with使用render如下。

Solution 1 解决方案1

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

The json result is: json的结果是:

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

Solution 2 解决方案2

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

The json result is: json的结果是:

{
  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. 我最终使用了RABL模板,这让我的生活变得更轻松。 If you need a guide on getting up and running with RABL quickly, I've put one together: 如果你需要一本关于快速启动和运行RABL的指南,我会把它放在一起:

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

where returns an array, so your results make sense. where返回一个数组,因此您的结果是有意义的。 You'd need where().first , or since it's just an ID lookup, simply use find . where().first需要where().first或者因为它只是一个ID查找,所以只需使用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? 你现在可能已经解决了它的问题,但如果你还在使用probs,你可以这样做吗?

@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: 关于'渴望加载协会'的api中有一个非常好的部分:

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

As nixterrimus pointed out RABL is really the way to go. 正如nixterrimus指出的那样,RABL真的是要走的路。 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. 我是相对较新的rails并没有使用这些技术(to_json等)但是做了一些研究并实际使用RABL,我不禁想知道为什么它不是Rails的原生特性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM