具有继承资源的嵌套对象
首先,我喜欢继承资源
请考虑以下因素:
class Job < ActiveRecord::Base
has_many :inputs, dependent: :destroy
has_one :output
end
class JobsController < InheritedResources::Base
respond_to :json
end
当我请求 jobs/1.json 时,我只获取作业对象的 JSON。我还想要包含输入和输出。我通常通过以下方式实现此目的:
job.to_json(include: [:inputs,:output])
我的问题是使用 IR 实现此目的的最佳方法是什么?现在,我只覆盖 show,但我想知道是否有更优雅的方法?
谢谢!
First off, I love inherited_resources
Consider the following:
class Job < ActiveRecord::Base
has_many :inputs, dependent: :destroy
has_one :output
end
class JobsController < InheritedResources::Base
respond_to :json
end
When I request jobs/1.json I just get the JSON of the job object. What I want is also the inputs and output to be included. I normally achieve this by:
job.to_json(include: [:inputs,:output])
My question is what is the best way to achieve this with IR? For now, I'll just overwrite show, but I wanted to know if there was a more elegant way?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
@corroded 让我走上了正轨。答案是覆盖模型上的 as_json 。
具体来说,我做了以下工作:
@corroded put me on the right track. The answer is to overwrite as_json on the model.
Specifically I did the following: