-
Notifications
You must be signed in to change notification settings - Fork 900
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
Deleting using Has-Many-Through Associations problem #113
Comments
It's most probably because Rails has evolved since that patch. If you look at Rails' current implementation you can see it's changed quite a bit. You could try removing the patch and setting up your has-many-through association with the |
I did try |
I wonder whether that's sufficient: this line, which is called along the way, also uses |
I thought it would call In the end, it'd be something like this: def delete_records(records, method)
ensure_not_nested
scope = through_association.scoped.where(construct_join_attributes(*records))
case method
when :destroy
count = scope.destroy_all.length
when :nullify
count = scope.update_all(source_reflection.foreign_key => nil)
else
count = scope.destroy_all.length
# count = scope.delete_all
end
delete_through_records(records)
if through_reflection.macro == :has_many && update_through_counter?(method)
update_counter(-count, through_reflection)
end
update_counter(-count)
end |
Yes, but the |
Not sure why, but it's working for me with that line replaced. Item.rb class Item < ActiveRecord::Base
belongs_to :unit
has_many :item_categories, dependent: :destroy
has_many :categories, through: :item_categories
has_paper_trail
end Category.rb class Category < ActiveRecord::Base
has_many :item_categories, dependent: :destroy
has_many :items, through: :item_categories
has_paper_trail
end ItemCategory.rb class ItemCategory < ActiveRecord::Base
belongs_to :item
belongs_to :category
has_paper_trail
end _form.html.haml = form_for @item do |f|
- Category.all.each do |category|
= check_box_tag :category_ids, category.id, @item.categories.include?(category), :name => 'item[category_ids][]'
items_controller.rb def update
# Set to an empty array if no value is set
params[:item][:category_ids] ||= []
@item = Item.find(params[:id])
if @item.update_attributes(params[:item])
redirect_to @item
else
render action: "edit"
end
end Without
With the line replaced:
|
It seems like this one is working: module ActiveRecord
# = Active Record Has Many Through Association
module Associations
class HasManyThroughAssociation < HasManyAssociation #:nodoc:
alias_method :ori_delete_records, :delete_records
def delete_records(records, method)
method ||= :destroy
ori_delete_records(records, method)
end
end
end
end From the log:
|
That looks good. I'll update the README. Thanks! |
That patch looks extremely dangerous because it will change the behavior of you application. Records that you expected to get nullified or deleted will instead by destroyed. If you want the same behavior, just attach dependent => :destroy to your association |
Hi there
From the README, I need to use the monkey patch to solve the Has-Many-Through Associations problems when deleting the data. However, I'm getting this error:
Am I doing it wrong or is it because Rails have newer codes?
The text was updated successfully, but these errors were encountered: