Add virtual attribute to json output

后端 未结 7 1253
醉梦人生
醉梦人生 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:40

    This will do, without having to do some ugly overridings. If you got a model List for example, you can put this in your controller:

      render json: list.attributes.merge({
                                           finished_items: list.finished_items,
                                           unfinished_items: list.unfinished_items
                                         })
    
    0 讨论(0)
提交回复
热议问题