westonganger/paper_trail-association_tracking

Associations will be doubled after reifying

andreas-it-dev opened this issue · 2 comments

Hi all,

i am not sure if its a bug or i made a mistake, though apologies if this isnt a bug.

i am using

paper_trail (10.0.1)
paper_trail-association_tracking (1.0.0)

with the following db schema (i removed the irrelevant tables):

ActiveRecord::Schema.define(version: 2018_10_24_090849) do

  # These are extensions that must be enabled in order to support this database
  enable_extension "plpgsql"

  create_table "broker_instruments", force: :cascade do |t|
    t.bigint "instrument_id"
    t.bigint "broker_id"
    t.string "symbol"
    t.float "commissions"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["broker_id"], name: "index_broker_instruments_on_broker_id"
    t.index ["instrument_id"], name: "index_broker_instruments_on_instrument_id"
  end

  create_table "brokers", force: :cascade do |t|
    t.string "name"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

  create_table "instruments", force: :cascade do |t|
    t.string "description"
    t.string "category"
    t.float "tick_size"
    t.float "currency_value_per_tick"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.string "currency"
  end

  create_table "version_associations", force: :cascade do |t|
    t.integer "version_id"
    t.string "foreign_key_name", null: false
    t.integer "foreign_key_id"
    t.index ["foreign_key_name", "foreign_key_id"], name: "index_version_associations_on_foreign_key"
    t.index ["version_id"], name: "index_version_associations_on_version_id"
  end

  create_table "versions", force: :cascade do |t|
    t.string "item_type", null: false
    t.integer "item_id", null: false
    t.string "event", null: false
    t.string "whodunnit"
    t.text "object"
    t.datetime "created_at"
    t.integer "transaction_id"
    t.index ["item_type", "item_id"], name: "index_versions_on_item_type_and_item_id"
    t.index ["transaction_id"], name: "index_versions_on_transaction_id"
  end

  add_foreign_key "broker_instruments", "brokers"
  add_foreign_key "broker_instruments", "instruments"
end

my relevant models are:

class Instrument < ApplicationRecord
  has_many :broker_instruments, dependent: :destroy
  has_many :brokers, through: :broker_instruments

  has_paper_trail

  validates :description, :category, :tick_size, :currency_value_per_tick, :currency, presence: true
end
class Broker < ApplicationRecord
  has_many :broker_instruments, dependent: :destroy
  has_many :instruments, through: :broker_instruments

  has_paper_trail

  validates :name, presence: true
end
class BrokerInstrument < ApplicationRecord
  belongs_to :instrument
  belongs_to :broker

  has_paper_trail
end

and i have a Versions controller (based on Ryan Bates' railscast)

class VersionsController < ApplicationController
  def revert
    @version = PaperTrail::Version.find(params[:id])
    if @version.reify
      @version.reify(has_many: true).save!(validate: false)
    else
      @version.item.destroy
    end
    redirect_back fallback_location: root_path, notice: "Successfully undid #{@version.event}!"
  end
end

so, i delete an instrument, associations are removed.. when i reify that instrument i get back my original associations PLUS a pair of new ones (but with (for example) an empty commissions field, even though the original ones had set commissions, only the first set of associations will have those set commissions)

any comment is appreciated!

thanks a lot,
Andreas

anyone?

hi @awunder could you please give the steps to reproduce your issue in Ruby? I mean writing code lines with expected / actual behavior on comments?