defaults for to_json in Rails with :include

后端 未结 5 1035
悲&欢浪女
悲&欢浪女 2020-12-29 23:24

Let us say I have a model Post which belongs to a User. To convert to json, I do something like this

@reply.to_json(:include => {:user => {:only =>         


        
5条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-30 00:20

    You could add another method to ActiveRecord::Base, like self.to_minimal_json(*only_fields) and build your .to_json call there.

    unspectacular, but may work.

提交回复
热议问题