I'm using Associated Audits on a has_many through relation with Collective Idea's audited gem. I see create audits for the through model being added, but I do not see any audits when that relation is removed.
Here are my 3 models. A Post can be in multiple Categories.
app/models/post.rb
class Post < ActiveRecord::Base
audited
has_associated_audits
has_many :categorizations, dependent: :destroy
has_many :categories, through: :categorizations
end
app/models/category.rb
class Category < ActiveRecord::Base
audited
has_associated_audits
has_many :categorizations, dependent: :destroy
has_many :posts, through: :categorizations
end
app/models/categorization.rb
class Categorization < ActiveRecord::Base
audited
audited associated_with: :post
audited associated_with: :category
belongs_to :category
belongs_to :post
end
My Post form has a pile of checkboxes for categorization:
<%= f.association :categories, as: :check_boxes, collection: Category.order(:name), label_method: :name, value_method: :id, label: false %>
Post and check a box for a Category, I do see a new audit entry with a create value in the audit's action field.Post and uncheck a box for a Category, I do not see a new audit entry.I do see destroy audits for both the Post and the Categorization auditable_type fields when I delete a Post, so that aspect works nicely.
has_many through documentation to follow, so I guessed a bit.Potentially related to this Rails issue, I had to swap out my dependent: :destroy lines:
app/models/post.rb
class Post < ActiveRecord::Base
audited
has_associated_audits
has_many :categorizations
has_many :categories, through: :categorizations, dependent: :destroy
end
app/models/category.rb
class Category < ActiveRecord::Base
audited
has_associated_audits
has_many :categorizations
has_many :posts, through: :categorizations, dependent: :destroy
end
With this setup in place, I'm seeing audits for both adding and removing relations.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With