Rails 3.0.4 中关系模型的 json 渲染的奇怪问题

发布于 2024-10-20 15:42:18 字数 906 浏览 6 评论 0原文

我通过 one_to_many 建立了关系:虽然关系:

  has_one :todays_order, :through => :patient_orders, :source => :daily_order ,:conditions => ["order_for_date = ?", Date.today]

但是当我想在这样的控制器中渲染它时:

    respond_with(@daily_order = Patient.find(params[:patient_id]).todays_order)

我会得到以下响应:

{“marked_for_destruction”=>假, "changed_attributes"=>{}, “属性”=> {“additional_information”=>“....”,“id”=>“594369222”}, “只读”=>假, “错误”=>{}, "previously_changed"=>{}, “摧毁”=>假, "attributes_cache"=>{}, “new_record”=>假}

但输出应该是这样的:

{“additional_information”=>“....”,“id”=>“594369222”}

这里出了什么问题?

PS:您可以找到完整的控制器和模型:http://pastebin.com/VSbvesTn

I've got a relationship through a one_to_many :though relationship:

  has_one :todays_order, :through => :patient_orders, :source => :daily_order ,:conditions => ["order_for_date = ?", Date.today]

But when I want to render that in a controller like that:

    respond_with(@daily_order = Patient.find(params[:patient_id]).todays_order)

I'll get the following response:

{"marked_for_destruction"=>false,
"changed_attributes"=>{},
"attributes"=>
{"additional_information"=>"....", "id"=>"594369222"},
"readonly"=>false,
"errors"=>{},
"previously_changed"=>{},
"destroyed"=>false,
"attributes_cache"=>{},
"new_record"=>false}

But output should be something like that:

{"additional_information"=>"....", "id"=>"594369222"}

Whats wrong here?

P.S.: You can find the complete Controller and Model: http://pastebin.com/VSbvesTn

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

猫九 2024-10-27 15:42:18

通过不渲染 Patient.todays_order“关系”解决了问题,而是渲染 AR 查询的结果。
所以我认为你不能直接在 Rails 中渲染关系对象。

solved the problem by not rendering the patient.todays_order "relationship", instead i'm rendering result of an AR query.
So I think you cant render a relationship object directly in rails.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文