Non persistent ActiveRecord model attributes

后端 未结 5 1252
暗喜
暗喜 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:02

    Of course use good old ruby's attr_accessor. In your model:

    attr_accessor :foo, :bar
    

    You'll be able to do:

    object.foo = 'baz'
    object.foo #=> 'baz'
    

提交回复
热议问题