非持久ActiveRecord模型属性 [英] Non persistent ActiveRecord model attributes
本文介绍了非持久ActiveRecord模型属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我要添加到现有的模型,一些属性不需要依然存在,甚至映射到数据库列。 有没有一种解决方案,指定这样的事?
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 ?
推荐答案
当然,使用好老的红宝石的 attr_accessor
。在你的模型:
Of course use good old ruby's attr_accessor
. In your model:
attr_accessor :foo, :bar
您将能够做的:
object.foo = 'baz'
object.foo #=> 'baz'
这篇关于非持久ActiveRecord模型属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文