Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/53.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 如何在rails模型中保存历史记录_Ruby On Rails_Ruby - Fatal编程技术网

Ruby on rails 如何在rails模型中保存历史记录

Ruby on rails 如何在rails模型中保存历史记录,ruby-on-rails,ruby,Ruby On Rails,Ruby,这里有三个问题,都以粗体设置 我有一个人事数据库,历史数据也存储在其中。 每个表都有两列start和stop,指示此事实的有效期 当记录的任何字段发生更改时,将使用start=today和stop=nil创建新记录, 旧记录被赋予一个stop=today-1(或者无论更改的生效日期是什么而不是今天) 当要删除记录时,我们只需设置stop=today,而不是删除 这样,就很容易 提供任何员工的历史记录 对某一特定日期的事情做一个快照 首先,这种模式似乎很常见,可能我不需要重新发明轮子。 然而,

这里有三个问题,都以粗体设置

我有一个人事数据库,历史数据也存储在其中。 每个表都有两列
start
stop
,指示此事实的有效期

当记录的任何字段发生更改时,将使用
start=today
stop=nil
创建新记录, 旧记录被赋予一个
stop=today-1
(或者无论更改的生效日期是什么而不是今天)

当要删除记录时,我们只需设置
stop=today
,而不是删除

这样,就很容易

  • 提供任何员工的历史记录
  • 对某一特定日期的事情做一个快照
首先,这种模式似乎很常见,可能我不需要重新发明轮子。 然而,在我的搜索中,我发现了一些被称为
可审计的
书面记录的宝石。
乍一看,他们似乎没有做我想做的事,但也许我错了。
你知道有一种宝石具有类似的功能吗?

如果我没有找到一个解决方案,那么对于我的目的来说,有几个方法就足够了,这些方法适用于我的所有模型,比如

def when(date)
    self.where("start <= ? and ? <= stop", ondate, ondate)
end
定义时间(日期)
self.where(“start在我看来,这是一个很好的机会来使用。你可以这样写:

History.rb

require 'active_support/concern'

module History
  extend ActiveSupport::Concern
  included do
    def self.when(date)
      self.where("start <= ? and ? <= stop", ondate, ondate)
    end
  end
end
需要“主动支持/关注”
模块历史记录
扩展ActiveSupport::关注点
包括做
定义自何时(日期)

self.where(“开始
让我的所有模型继承MyRecord,但这会破坏一切。
什么破坏了?另一种方法是将您的通用代码放在一个模块中,并将其包含在所有需要它的模型中,但是的,您的继承解决方案应该已经起作用了。@emaillenin“什么破坏了?”现在我不记得了,所以我试着复制。第一个问题:这个文件my_record.rb去哪里了?在模型中,它试图找到一个具有该名称的表。在helpers中,它仍然有相同的抱怨。
require 'active_support/concern'

module History
  extend ActiveSupport::Concern
  included do
    def self.when(date)
      self.where("start <= ? and ? <= stop", ondate, ondate)
    end
  end
end
class SomeClass < ActiveRecord::Base
  include History
end