How to add new attribute to ActiveRecord

后端 未结 8 1798
佛祖请我去吃肉
佛祖请我去吃肉 2021-02-20 01:18

After getting all values from model, I want to add another custom attribute to the ActiveRecord class (this attribute is not a column in db) so that I could use it in view, but

8条回答
  •  佛祖请我去吃肉
    2021-02-20 01:33

    If you want this only for your views and do not have any other purpose then you need not to add attr_accessor

    @test.all.select('tests.*, "added string" as newattr')
    

    here you are adding newattr attribute for query output of ActiveRecord with a value 'added string'

提交回复
热议问题