I'm using this code to convert a model to json. If i try to use an include 2nd level like this:
p = Product.includes({ :variants => { :stocks => :size } }).where(:id => params[:id]).first
render :json => p.variants.to_json(:include => { :stocks => { :include => :size } })
I receive this error:
undefined method `macro' for nil:NilClass
How I can solve that?开发者_如何学运维
Try this:
render :json => p.variants.map { |v| v.as_json(:include => {:stocks => {:include => :size}}) }
Info about Object#as_json
/to_json
here.
精彩评论