【发布时间】:2015-06-19 03:56:04
【问题描述】:
对于任何 ActiveRecord 模型,我都可以访问 changed 属性以获取已更改的字段数组。
例如:
a = Article.find(1)
a.title = "New Title"
a.changed => returns ["title"]
有没有办法让我自己设置“更改”属性?假设我想让它为空。原因是,我实际上是从缓存中读取文章属性(Redis,但这并不重要),而不是从数据库中读取。
很遗憾,我做不到
def getArticleFromCache
json = getJsonFromCache()
a = Article.new
a.attributes = json
a.changed = [] #doesn't work, changed contains ALL the attributes in the json
return a
end
我能做什么?
换句话说,我希望调用 getArticleFromCache 的人看起来好像是从数据库中获取的一样。
【问题讨论】:
标签: ruby-on-rails ruby activerecord