-
Notifications
You must be signed in to change notification settings - Fork 902
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Does not track has_many :through
deletion
#883
Comments
I found out about issue #113 . But still the behaviour is not what I would expect. The # Use this template to report PaperTrail bugs.
# Please include only the minimum code necessary to reproduce your issue.
require "bundler/inline"
# STEP ONE: What versions are you using?
gemfile(true) do
ruby "2.3.0"
source "https://rubygems.org"
gem "activerecord", "5.0.0"
gem "minitest", "5.9.0"
gem "paper_trail", "5.2.0", require: false
gem "sqlite3"
end
require "active_record"
require "minitest/autorun"
require "logger"
ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:")
ActiveRecord::Base.logger = nil
ActiveRecord::Schema.define do
# STEP TWO: Define your tables here.
create_table :profiles, force: true do |t|
t.timestamps null: false
end
create_table :profile_users, force: true do |t|
t.references :profile
t.references :user
end
create_table :users, force: true do |t|
t.timestamps null: false
end
create_table :versions 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, limit: 1_073_741_823
t.text :object_changes, limit: 1_073_741_823
t.integer :transaction_id
t.datetime :created_at
end
add_index :versions, [:item_type, :item_id]
add_index :versions, [:transaction_id]
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, [:foreign_key_name, :foreign_key_id],
name: "index_version_associations_on_foreign_key"
end
ActiveRecord::Base.logger = Logger.new(STDOUT)
require "paper_trail/config"
# STEP THREE: Configure PaperTrail as you would in your initializer
PaperTrail::Config.instance.track_associations = false
module ActiveRecord
module Associations
class HasManyThroughAssociation < HasManyAssociation #:nodoc:
alias_method :original_delete_records, :delete_records
def delete_records(records, method)
method ||= :destroy
original_delete_records(records, method)
end
end
end
end
require "paper_trail"
# STEP FOUR: Define your AR models here.
class User < ActiveRecord::Base
has_paper_trail
has_many :profile_users
has_many :profiles, through: :profile_users
end
class Profile < ActiveRecord::Base
has_paper_trail
has_many :profile_users
has_many :users, through: :profile_users
end
class ProfileUser < ActiveRecord::Base
has_paper_trail
belongs_to :profile
belongs_to :user
end
# STEP FIVE: Please write a test that demonstrates your issue.
class BugTest < ActiveSupport::TestCase
def test_1
user = User.create
profile = Profile.create
assert_difference(-> { PaperTrail::Version.count }, +1) {
profile.users = [user]
profile.save
}
assert_equal <<YAML, PaperTrail::Version.last.object_changes
---
id:
-
- 1
profile_id:
-
- 1
user_id:
-
- 1
YAML
assert_difference(-> { PaperTrail::Version.count }, +1) {
profile.users = []
profile.save
}
assert_equal <<YAML, PaperTrail::Version.last.object_changes
---
id:
- 1
-
profile_id:
- 1
-
user_id:
- 1
-
YAML
end
end =>
|
Hi Arthur, thanks for testing the experimental association-tracking features.
We only store attributes in the |
Hi Jared, I don't really want to use the associations feature. I am using As in: ProfileUser record with Please correct me if I am wrong, maybe there is another solution for this. And thanks for your prompt help 👍 |
This is the first I've heard of overriding AR's |
I know overriding Wouldn't user = User.create
profile = Profile.create(users: [user])
profile.users = []
profile.save ActiveRecord takes care of calling As far as I can see, that's a paper_trail issue. I haven't found a good hint yet, why that is, though. :) |
When the associated objects are destroyed, a record representing each deletion will be inserted into |
Alright. Thanks for the hint! But the # Use this template to report PaperTrail bugs.
# Please include only the minimum code necessary to reproduce your issue.
require "bundler/inline"
# STEP ONE: What versions are you using?
gemfile(true) do
ruby "2.3.0"
source "https://rubygems.org"
gem "activerecord", "5.0.0"
gem "minitest", "5.9.0"
gem "paper_trail", "5.2.0", require: false
gem "sqlite3"
gem "pry"
gem "pry-byebug"
end
require "active_record"
require "minitest/autorun"
require "logger"
ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:")
ActiveRecord::Base.logger = nil
ActiveRecord::Schema.define do
# STEP TWO: Define your tables here.
create_table :profiles, force: true do |t|
t.timestamps null: false
end
create_table :profile_users, force: true do |t|
t.references :profile
t.references :user
end
create_table :users, force: true do |t|
t.timestamps null: false
end
create_table :versions 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, limit: 1_073_741_823
t.text :object_changes, limit: 1_073_741_823
t.integer :transaction_id
t.datetime :created_at
end
add_index :versions, [:item_type, :item_id]
add_index :versions, [:transaction_id]
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, [:foreign_key_name, :foreign_key_id],
name: "index_version_associations_on_foreign_key"
end
ActiveRecord::Base.logger = Logger.new(STDOUT)
require "paper_trail/config"
# STEP THREE: Configure PaperTrail as you would in your initializer
PaperTrail::Config.instance.track_associations = false
require "paper_trail"
# STEP FOUR: Define your AR models here.
class User < ActiveRecord::Base
has_paper_trail
has_many :profile_users
has_many :profiles, through: :profile_users, dependent: :destroy
end
class Profile < ActiveRecord::Base
has_paper_trail
has_many :profile_users
has_many :users, through: :profile_users, dependent: :destroy
end
class ProfileUser < ActiveRecord::Base
has_paper_trail
belongs_to :profile
belongs_to :user
end
# STEP FIVE: Please write a test that demonstrates your issue.
class BugTest < ActiveSupport::TestCase
def test_1
user = User.create
profile = Profile.create
assert_difference(-> { PaperTrail::Version.count }, +1) {
profile.users = [user]
profile.save
}
assert_equal <<YAML, PaperTrail::Version.last.object_changes
---
id:
-
- 1
profile_id:
-
- 1
user_id:
-
- 1
YAML
assert_difference(-> { PaperTrail::Version.count }, +1) {
profile.users = []
profile.save
}
assert_equal <<YAML, PaperTrail::Version.last.object_changes
---
id:
- 1
-
profile_id:
- 1
-
user_id:
- 1
-
YAML
end
end =>
|
Because you are using |
The assertion is about the Version for the
|
Thanks for the clarification, I think I see what you're after now. We currently set Unless Ben says otherwise, feel free to implement this new feature in |
Ah! I see, my bad, The information in Adding the I am sorry for the confusion! Thanks for your help, I owe you a beer or two. 👍 |
I have switched from
has_and_belongs_to_many
tohas_many :through
, so that I can track theProfileUser
association in separateVersion
records. Unfortunatelypaper_trail
does not track the deletion of relatedusers
when I change theusers
Array in myprofile
record.=>
The text was updated successfully, but these errors were encountered: