Rails 3.0.4 中关系模型的 json 渲染的奇怪问题
我通过 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
通过不渲染 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.