Merge pull request #2733 from kamilbielawski/destroy_category_group_when_category_is_destroyed
FIX: destroy CategoryGroup when Category or Group is destroyed
This commit is contained in:
commit
a2d4347573
|
@ -19,7 +19,7 @@ class Category < ActiveRecord::Base
|
||||||
has_many :category_featured_users
|
has_many :category_featured_users
|
||||||
has_many :featured_users, through: :category_featured_users, source: :user
|
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
|
has_many :groups, through: :category_groups
|
||||||
|
|
||||||
validates :user_id, presence: true
|
validates :user_id, presence: true
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
class Group < ActiveRecord::Base
|
class Group < ActiveRecord::Base
|
||||||
include HasCustomFields
|
include HasCustomFields
|
||||||
|
|
||||||
has_many :category_groups
|
has_many :category_groups, dependent: :destroy
|
||||||
has_many :group_users, dependent: :destroy
|
has_many :group_users, dependent: :destroy
|
||||||
|
|
||||||
has_many :categories, through: :category_groups
|
has_many :categories, through: :category_groups
|
||||||
|
|
|
@ -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
|
Loading…
Reference in New Issue