Rails 3 respond_with json 问题
生成一些 json 时遇到问题。我正在尝试渲染一个 单个活动记录结果转为 json,如下所示:
@data = User.find(1)
respond_with(@data, :include => :status)
json 结果是:
{
-user: {
address: null
email: "[email protected]"
first_name: "Test"
last_name: "Man"
status_id: 1
username: "testguy"
status: { }
}
}
那么问题是什么?问题是 :include=>:status 似乎 不带出这种关系。在我的用户模型中我有一个 属于:状态。我如何让它在单个结果集上工作?
当我这样做时:
@data = User.where("id = 1")
respond_with(@data, :include => :status)
关系以这种方式显示在 json 结果集中。但其 在我不想要的对象数组中。
有什么想法吗?
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: "[email protected]"
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我假设您想要包含的 状态 不是 http 状态代码之一(200、201 等),也不是以任何方式与 User 对象关联的对象,而是您自己的变量。
使用 Rails 3.0.10 和 Ruby 1.9.2,您可能会找到适合您的这两种解决方案之一。使用
render
代替respond_with
,如下所示。解决方案 1
json 结果是:
解决方案 2
json 结果是:
我希望这就是您正在寻找的内容。
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
userender
as follows.Solution 1
The json result is:
Solution 2
The json result is:
I hope this is what you were looking for.
听起来您在使用内置 Rails 序列化时遇到了一些与我相同的痛苦。我最终使用了 RABL 模板,这让我的生活变得更加轻松。如果您需要快速启动和运行 RABL 的指南,我已经整理了一份:
http://blog.dcxn.com/2011/06/22/rails-json-templates-through-rabl/
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
返回一个数组,因此您的结果有意义。您需要where().first
,或者由于它只是 ID 查找,因此只需使用find
即可。这是很久以前的事了,但这仍然是最简洁的答案:
where
returns an array, so your results make sense. You'd needwhere().first
, or since it's just an ID lookup, simply usefind
.This was a long time ago, but still, this is the most concise answer:
伙计,你现在可能已经解决了这个问题,但是如果你仍然遇到问题,如果你愿意的话,你可以这样做吗?
如果这不起作用,则您的关联可能有问题。 api 中有一个关于“关联的预加载”的非常好的部分:
http://apidock。 com/rails/ActiveRecord/Associations/ClassMethods
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?
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
正如 nixterrimus 指出的那样,RABL 确实是一条出路。它简单、干净、优雅。我对 Rails 比较陌生,没有使用过这些技术(to_json 等),但在做了一些研究并实际使用 RABL 后,我不禁想知道为什么它不是 Rails 的原生功能。
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.