Ruby on Rails渲染对象数组和元素数

时间:2015-03-30 13:27:52

标签: ruby-on-rails

我有一个使用此渲染的rails方法:

render :json => @boats, :include => { :mainPhoto => {:only => [:id, :mime]},
                                           :boat_model => { :only => :name, :include =>  {:boat_type => { :only => :name}}}}

我可以在此响应中包含numTotal变量吗?

1 个答案:

答案 0 :(得分:0)

您需要使用as_json方法在分离的JSON密钥中序列化@boats

render :json => { numTotal: boats.size, boats: @boats.as_json(:include => { :mainPhoto => {:only => [:id, :mime]},
                                           :boat_model => { :only => :name, :include =>  {:boat_type => { :only => :name}}}})