Add virtual attribute to json output

后端 未结 7 1449
醉梦人生
醉梦人生 2020-11-27 17:05

Let\'s say I have an app that handles a TODO list. The list has finished and unfinished items. Now I want to add two virtual attributes to the list object; the count of fini

7条回答
  •  囚心锁ツ
    2020-11-27 17:19

    With Rails 4, you can do the following -

    render json: @my_object.to_json(:methods => [:finished_items, :unfinished_items])
    

    Hope this helps somebody who is on the later / latest version

提交回复
热议问题