小编Tha*_*ick的帖子

缩短respond_with(:include => xxx)

我正在寻找一种方法来缩短:include =>:在respond_with中生成json的child.

这是一个例子,不确定它是否可能,但我想知道.

在控制器中:

@p = Parent.where('id = ?', params[:id])
respond_with(@p, :include => {:child1 => {}, :child2 => {}, :child3 => {:include => :grandchild1}})
Run Code Online (Sandbox Code Playgroud)

在我定义实例时,是否有一些包含这些内容?

也许是这样的:

@p = Parent.includes(:child1, :child2, :child3, :grandchild1).where('id = ?', params[:id])
respond_with(@p)
Run Code Online (Sandbox Code Playgroud)

基本上,我正在尝试干掉我的代码...我不想一直反复输入包含哈希...是否有一些只包括所有子对象的一次调用?

ruby ruby-on-rails include arel respond-with

2
推荐指数
1
解决办法
2504
查看次数

标签 统计

arel ×1

include ×1

respond-with ×1

ruby ×1

ruby-on-rails ×1