停止ActiveRecord的节约了序列化列,即使没有改变 [英] Stop ActiveRecord saving a serialized column even if not changed

查看:91
本文介绍了停止ActiveRecord的节约了序列化列,即使没有改变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是非常相似的 Rails的部分更新的问题,哈希的,但这个问题并没有真正得到回答恕我直言。

This is very similar to Rails partial updates problem with hashes , but the question has not really been answered IMHO.

问题是这样的:我有一个序列化的列模型:

The problem is this: I have a model with a serialized column:

class Import < AR::Base
  serialize :data

在我的情况下,这些数据将,并应,而不是先保存/创建模型之后更改。所以我想禁用的AR始终保存序列化列的功能(通常是一个好主意,因为它无法检测这些变化)。我想禁用保存,因为数据可能相当大,模型会经常更新。

In my case, this data will, and should, not change after the first save/creation of the model. So I want to disable the feature of AR that always saves serialized columns (which is normally a good idea, as it can't detect those changes). I want to disable the saving because the data can be quite large, and the model will be updated frequently.

我已经尝试过的monkeypatching到ActiceRecord :: AttributeMethods ::肮脏的是这样的:

I've already tried monkeypatching into ActiceRecord::AttributeMethods::Dirty like this:

class Import
  def update(*)
    if partial_updates?
      super(changed | (attributes.keys & (self.class.serialized_attributes.keys - ["data"])))
    else
     super
   end
 end

但这似乎没有任何效果。任何人有更好的主意?

but this seems to have no effect. Anybody got a better idea ?

这是在Rails的3.0.12

This is under Rails 3.0.12

推荐答案

我最后怎么了做什么,即使它不是一个真正的答案,原来的问题,如下:

What I ended up doing, even though it's not really an answer to the original question, is the following:

class Import < AR::Base
  belongs_to :storage

class Storage < AR::Base
  serialize :data

...即。移动数据列进自己的模型,和准与原始模型。这其实是概念上有点清洁。

...i.e. moving the data column into a model of its own, and associate that with the original model. Which is actually conceptually somewhat cleaner.

这篇关于停止ActiveRecord的节约了序列化列,即使没有改变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆