Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8305313
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T18:05:22+00:00 2026-06-08T18:05:22+00:00

I have a model with member, group, membership and user. The data is organized

  • 0

I have a model with “member”, “group”, “membership” and “user”. The data is organized in a tree structure with groups that have members associated to them. Members without any group associations are considered orphaned and is of no use for the application.

When a user destroys a group there should not be left any orphaned members. In other words: a member should be destroyed if and only if the last group association is removed. Preferably this should happen in a single transaction, but the most important aspect is that orphaned object is not accumulated in the database over time.

Only members and groups that are associated with the user should be removed. Groups, members and memberships owned by other users should not be affected at all. (One might argue that a global cleanup method could be run anytime, but I want to isolate destructive operations to only affect the current users objects.)

My question: What is the most efficient and elegant way to implement this functionality in Rails 3? My current implementation, illustrated by the simplified model described here, does not remove a member from the database unless the user manually deletes it (or that the entire user and all his/her data is removed by cascade deletion.)

class User < ActiveRecord::Base
  has_many :groups, :foreign_key => 'owner_id', :dependent => :delete_all
  has_many :members, :foreign_key => 'owner_id', :dependent => :destroy
end

class Member < ActiveRecord::Base
  belongs_to :owner, :class_name => 'User'    
  has_many :memberships, :dependent => :destroy
  has_many :groups, :through => :memberships
end

class Membership < ActiveRecord::Base
  belongs_to :member
  belongs_to :group
end

class Group < ActiveRecord::Base
  belongs_to :owner, :class_name => 'User'
  belongs_to :parent, :class_name => 'Group'    
  has_many :groups, :foreign_key => 'parent_id', :dependent => :destroy
  has_many :memberships, :dependent => :destroy
  has_many :members, :through => :memberships
end
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T18:05:23+00:00Added an answer on June 8, 2026 at 6:05 pm

    I solved this by adding a callback in the Membership class, which notifies a Member when the membership is destroyed. The member object then destroys itself if it no longer has any group associations.

    class Membership < ActiveRecord::Base
      belongs_to :member
      belongs_to :group
    
      after_destroy :notify_member
    
      def notify_member
        member.destroy_if_empty_groups
      end
    end
    
    class Member < ActiveRecord::Base
      belongs_to :owner, :class_name => 'User'    
      has_many :memberships, :dependent => :destroy
      has_many :groups, :through => :memberships
    
      def destroy_if_empty_groups
        if groups.count == 0
          self.destroy
        end
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a backbone model called Member - this contains membership data such as
I have a user, membership and group model. A user has many memberships, and
I have the model Member that contains all informations about the registered member on
I have a Users model that can have many Groups through Memberships. If a
I have the models User and Group, and the join table Membership which uses
I have the following Model: class Group(models.Model): member = models.ManyToManyField(Player, through='GroupMember') name = models.CharField(max_length=20,
I have a member model with a reset_token method (which assigns a user a
Say I have the good old person/group/membership relationship class Person(models.Model): name = models.CharField(max_length=128) def
Model Member belongs_to Discipline , i.e. user can have a discipline but it is
I have 4 models, Message, Group, User, Membership class Group < ActiveRecord::Base has_many :memberships

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.