Non persistent ActiveRecord model attributes

后端 未结 5 1260
暗喜
暗喜 2020-12-10 00:20

I want to add to an existing model some attributes that need not be persisted, or even mapped to a database column. Is there a solution to specify such thing ?

5条回答
  •  长情又很酷
    2020-12-10 01:06

    In case anyone is wondering how to render this to the view, use the method arguments for the render method, like so:

    render json: {results: results}, methods: [:my_attribute]
    

    Please know that this only works if you set the attr_accessor on your model and set the attribute in the controller action, as the selected answer explained.

提交回复
热议问题