From 0231df9d5b36fefd84e1cf4a9c45f1686270d097 Mon Sep 17 00:00:00 2001 From: Benjamin Meichsner Date: Sun, 21 Sep 2014 12:47:58 +0200 Subject: [PATCH 1/2] Delete memberships when deleting associated groups. --- app/models/group.rb | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/app/models/group.rb b/app/models/group.rb index 50f6d63e..f83f5bea 100644 --- a/app/models/group.rb +++ b/app/models/group.rb @@ -1,11 +1,11 @@ -# Groups organize the User. +# Groups organize the User. # A Member gets the roles from the Group class Group < ActiveRecord::Base - has_many :memberships + has_many :memberships, dependent: :destroy has_many :users, :through => :memberships validates :name, :presence => true, :length => {:in => 1..25} - + attr_reader :user_tokens scope :undeleted, -> { where(deleted_at: nil) } @@ -14,7 +14,7 @@ class Group < ActiveRecord::Base def member?(user) memberships.find_by_user_id(user.id) end - + # Returns all NONmembers and a checks for possible multiple Ordergroup-Memberships def non_members User.natural_order.reject { |u| users.include?(u) } @@ -37,5 +37,3 @@ class Group < ActiveRecord::Base end end end - - From c750416b9e515f011e6b7d45f5d476f3b5beabf4 Mon Sep 17 00:00:00 2001 From: Benjamin Meichsner Date: Sun, 21 Sep 2014 12:54:01 +0200 Subject: [PATCH 2/2] Remove all memberships where belonging group was deleted. --- db/migrate/20140921104907_remove_stale_memberships.rb | 5 +++++ db/schema.rb | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) create mode 100644 db/migrate/20140921104907_remove_stale_memberships.rb diff --git a/db/migrate/20140921104907_remove_stale_memberships.rb b/db/migrate/20140921104907_remove_stale_memberships.rb new file mode 100644 index 00000000..ca55535c --- /dev/null +++ b/db/migrate/20140921104907_remove_stale_memberships.rb @@ -0,0 +1,5 @@ +class RemoveStaleMemberships < ActiveRecord::Migration + def up + Membership.where("group_id NOT IN (?)", Group.ids).delete_all + end +end diff --git a/db/schema.rb b/db/schema.rb index c8e9c41e..eb038a10 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20140521142651) do +ActiveRecord::Schema.define(version: 20140921104907) do create_table "article_categories", force: true do |t| t.string "name", default: "", null: false