使用Paper Trail跟踪关联

时间:2017-05-11 04:06:55

标签: ruby-on-rails ruby ruby-on-rails-5 paper-trail-gem

我正在尝试使用 paper trail gem,我正在尝试使用实验性功能(to track associations)。

首先,这是我的文件(解释下面):

迁移/ user_version:

class CreateUserVersions < ActiveRecord::Migration[5.0]
  def change
    create_table :user_versions do |t|
      t.string :item_type, null: false
      t.integer :item_id, null: false
      t.string :event, null: false
      t.string :whodunnit, null: false
      t.json :object
      t.json :object_changes, null: false
      t.index %i(item_type item_id)

      t.timestamps
    end
  end
end

migrate / add_transaction_id_column_to_versions(默认来自纸质记录):

# This migration and CreateVersionAssociations provide the necessary
# schema for tracking associations.
class AddTransactionIdColumnToVersions < ActiveRecord::Migration
  def self.up
    add_column :user_versions, :transaction_id, :integer
    add_index :user_versions, [:transaction_id]
  end

  def self.down
    remove_index :user_versions, [:transaction_id]
    remove_column :user_versions, :transaction_id
  end
end

migrate / create_version_associations(默认来自纸质记录):

# This migration and AddTransactionIdColumnToVersions provide the necessary
# schema for tracking associations.
class CreateVersionAssociations < ActiveRecord::Migration
  def self.up
    create_table :version_associations do |t|
      t.integer  :version_id
      t.string   :foreign_key_name, null: false
      t.integer  :foreign_key_id
    end
    add_index :version_associations, [:version_id]
    add_index :version_associations,
      %i(foreign_key_name foreign_key_id),
      name: "index_version_associations_on_foreign_key"
  end

  def self.down
    remove_index :version_associations, [:version_id]
    remove_index :version_associations,
      name: "index_version_associations_on_foreign_key"
    drop_table :version_associations
  end
end

型号:

模型/ paper_trail /版本:

module PaperTrail
  class Version < ActiveRecord::Base
    include PaperTrail::VersionConcern
    self.abstract_class = true
  end
end

models / user_version(用于存储用户的版本):

class UserVersion < PaperTrail::Version
  self.table_name = :user_versions

  belongs_to :user, foreign_key: :whodunnit
end

模型/用户:

class User < ApplicationRecord
  has_paper_trail class_name: 'UserVersion'

  has_many :addresses, inverse_of: :user
  accepts_nested_attributes_for :addresses, allow_destroy: true
end

模型/地址:

class Address < ApplicationRecord
  has_paper_trail # <- The error is here

  belongs_to :user, inverse_of: :addresses
end

初始化/ paper_trail:

PaperTrail.config.track_associations = true

说明:

您可能已经注意到,我使用的是自定义版本类,而不是默认版本...

当我尝试创建/更新现有的嵌套模型(在我的情况下使用嵌套属性的“地址”)时出现问题。

实际行为:它会保存用户第一个 地址,因此会引发以下错误:

  

TypeError(没有将nil隐式转换为String):

如果我从has_paper_trail 模型中删除了行Address,它可以100%正常工作而不会出现错误(当然,没有版本控制)。

我错过了什么吗?实验性功能是否与“父”自定义版本类兼容?

PS#1:请注意,我不是要尝试使用自定义版本类来跟踪关联,我正在使用默认 associations_versions +自定义版本class(在父级,在本例中为“User”),如文档中所述。

PS#2:我没有包含控制器 code,因为它是一个基本配置而且它正在工作......问题出在有纸质记录的模型中。

提前致谢。

0 个答案:

没有答案