Ruby on Rails - Render JSON for multiple models -
i trying render results more 1 model in json. following code in controller renders first result set:
def calculate_quote @moulding = moulding.find(params[:id]) @material_costs = materialcost.all respond_to |format| format.json { render :json => @moulding } format.json { render :json => @material_costs } end end
any appreciated, thanks.
one way create hash objects want render, , pass render method. so:
respond_to |format| format.json { render :json => {:moulding => @moulding, :material_costs => @material_costs }} end
if models aren't associated through active record, that's best solution.
if association exist, can pass :include
argument render call, so:
respond_to |format| format.json { render :json => @moulding.to_json(:include => [:material_costs])} end
note wouldn't have retrieve @material_costs
variable in section above if take approach, rails automatically load @moulding
variable.
Comments
Post a Comment