You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Papertrail自定义对象版本表报错:ArticleVersion无'event'属性

Fixing unknown attribute 'event' for ArticleVersion with Papertrail Custom Version Tables

Hey there! That error pops up because your custom ArticleVersion table is missing a required field Papertrail relies on to track version events. Let’s walk through the fixes step by step:

1. Ensure your custom version table includes all Papertrail required fields

Papertrail needs specific columns to store version data, and event is non-negotiable—it tracks what action (create/update/destroy) generated the version.

If you haven’t created the article_versions table yet, use this migration file:

class CreateArticleVersions < ActiveRecord::Migration[6.1]
  def change
    create_table :article_versions do |t|
      t.string   :item_type, null: false # Tracks the parent model class (e.g., "Article")
      t.integer  :item_id,   null: false # Tracks the parent record's ID
      t.string   :event,     null: false # This is the missing field! Values like "create", "update"
      t.text     :object                # Stores the full state of the record at the time
      t.text     :object_changes        # Stores only the changed attributes
      t.string   :whodunnit             # Optional: tracks who made the change (user ID/email)
      t.datetime :created_at            # Auto-set to when the version was created
    end

    # Add index for faster lookups of a record's versions
    add_index :article_versions, [:item_type, :item_id]
  end
end

If you already ran the migration and missed the event column, generate a new migration to add it:

class AddEventToArticleVersions < ActiveRecord::Migration[6.1]
  def change
    add_column :article_versions, :event, :string, null: false, default: "update"
    # The default value ensures existing records don't break if you have any
  end
end

Run the migration with rails db:migrate after creating either file.

2. Verify your model configurations

Double-check your Article model points to the custom version class correctly:

class Article < ActiveRecord::Base
  # Tell Papertrail to use your custom version model
  has_paper_trail class_name: "ArticleVersion"
end

Your ArticleVersion model just needs to be a basic ActiveRecord model (no extra Papertrail setup required):

class ArticleVersion < ActiveRecord::Base
  # Optional: add a back-reference to the parent Article if you need it
  belongs_to :article, foreign_key: :item_id, optional: true
end

3. Why this works

The event attribute is critical because Papertrail automatically sets it when creating a version—for example, when you create a new Article, Papertrail tries to write "create" to the event column of the corresponding ArticleVersion record. If that column doesn’t exist, you get the UnknownAttributeError you’re seeing.

Once you add the event field and confirm your model mappings, creating articles should work without that error, and Papertrail will properly track all version history in your custom table.

内容的提问来源于stack exchange,提问作者megha

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 08:45:38