From bf679f1626ba8aebca4a4e7d037941f2da8b64ac Mon Sep 17 00:00:00 2001 From: Kamil Bielawski Date: Sun, 31 Aug 2014 22:10:38 +0200 Subject: [PATCH] FIX: destroy CategoryGroup when Category or Group is destroyed --- app/models/category.rb | 2 +- app/models/group.rb | 2 +- ...groups_orphaned_by_removing_category_or_group.rb | 13 +++++++++++++ 3 files changed, 15 insertions(+), 2 deletions(-) create mode 100644 db/migrate/20140831191346_remove_category_groups_orphaned_by_removing_category_or_group.rb diff --git a/app/models/category.rb b/app/models/category.rb index 390f8f2d363..76825bcf650 100644 --- a/app/models/category.rb +++ b/app/models/category.rb @@ -19,7 +19,7 @@ class Category < ActiveRecord::Base has_many :category_featured_users has_many :featured_users, through: :category_featured_users, source: :user - has_many :category_groups + has_many :category_groups, dependent: :destroy has_many :groups, through: :category_groups validates :user_id, presence: true diff --git a/app/models/group.rb b/app/models/group.rb index 2f636d6f09a..14138ba148f 100644 --- a/app/models/group.rb +++ b/app/models/group.rb @@ -1,7 +1,7 @@ class Group < ActiveRecord::Base include HasCustomFields - has_many :category_groups + has_many :category_groups, dependent: :destroy has_many :group_users, dependent: :destroy has_many :categories, through: :category_groups diff --git a/db/migrate/20140831191346_remove_category_groups_orphaned_by_removing_category_or_group.rb b/db/migrate/20140831191346_remove_category_groups_orphaned_by_removing_category_or_group.rb new file mode 100644 index 00000000000..2e19870c2bc --- /dev/null +++ b/db/migrate/20140831191346_remove_category_groups_orphaned_by_removing_category_or_group.rb @@ -0,0 +1,13 @@ +class RemoveCategoryGroupsOrphanedByRemovingCategoryOrGroup < ActiveRecord::Migration + def up + execute "DELETE FROM category_groups + WHERE group_id NOT IN ( + SELECT groups.id FROM groups) + OR category_id NOT IN ( + SELECT categories.id FROM categories)" + end + + def down + raise ActiveRecord::IrreversibleMigration + end +end