要解决这个问题,我们可以使用Rails中的ActiveRecord枚举类型。这将允许我们将对象更改操作作为枚举值存储在数据库中。
在模型中,我们可以定义一个枚举类,例如:
class PaperTrail < ApplicationRecord
enum object_changes: [:created, :updated, :deleted]
end
然后,我们可以在控制器中使用此枚举值来创建新的PaperTrail对象:
class ObjectChangesController < ApplicationController
def create
@paper_trail = PaperTrail.new(paper_trail_params)
@paper_trail.object_changes = :created
# ...
end
end
这将使用 :created
枚举值创建新的PaperTrail对象。
当我们需要在视图中显示这些更改时,我们可以使用枚举值的名称:
<% if paper_trail.created? %>
Object created!
<% elsif paper_trail.updated? %>
Object updated!
<% elsif paper_trail.deleted? %>
Object deleted!
<% end %>
这样我们就可以使用枚举类型来管理纸质记录对象的更改。