0

I am making an application in which there is only model that is Project and in the view of the project i can see the table with the variables that generate automatically i.e created _at and updated_at. I want to keep track the updated_at variable means that if user updates the project 10 times it will show the updated_at variable 10 times so that i can track the project model.

4

1 回答 1

0

您将需要另一个模型来跟踪随时间的变化,并在创建项目模型时使用活动记录回调。

假设您正在通过一个名为 ProjectChanges 的模型跟踪您的更改

rails g model ProjectChange project_id:integer change:string when:datetime

然后将实现回调挂钩添加到您的项目模型中

class Project < ActiveRecord::Base
  has_many :project_changes
  after_create :record_update( 'create' )
  after_save :record_update( 'update' )
  ...
  def record_update( what_happened )
    # not forgetting to add attr_accessible to the ProjectChange class if you want to mass assign like this
    ProjectChange.new( { :change => what_happened, :when => Time.now } )
  end
end
于 2012-11-06T11:07:37.470 回答