How to add new attribute to ActiveRecord

后端 未结 8 1908
佛祖请我去吃肉
佛祖请我去吃肉 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:40
    @test.all
    
    @test.each do |elm|
        write_attribute(:newatt, "added string")
    end
    
    0 讨论(0)
  • 2021-02-20 01:45

    If it's really just temporary it doesn't have to be in the object:

    @test.all
    @test_temp = []
    
    @test.each do |elm|
        @test_temp << {:elm => elm, :newatt => 'added string'}
    end   
    

    Otherwise, there are also good answers here.

    0 讨论(0)
提交回复
热议问题