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 7897057
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T08:03:27+00:00 2026-06-03T08:03:27+00:00

I have users and projects resources and join table that connects them called project_members.

  • 0

I have users and projects resources and join table that connects them called project_members. I wish to have functionality when all users leave the project, the project destroys itself, something like spec below:

 75     it 'deletes the project when there are no more users on it' do
 76       lambda do
 77         project.users.clear 
 78       end.should change(Project, :count).by(-1)
 79     end

So far I came up with this line but don’t know where to put it…

@project.destroy if @project.users.empty?

EDIT: here are my models

User model (I’m using Devise)

  1 class User < ActiveRecord::Base
  2 
  3   has_many :synapses #aka project_members      
  4   has_many :projects, :through => :synapses
  5 
  6   # Include default devise modules. Others available are:
  7   # :token_authenticatable, :encryptable, :lockable, :timeoutable and :omniauthable
  8   devise :database_authenticatable, :registerable,
  9          :recoverable, :rememberable, :trackable, :validatable,
 10          :confirmable      
 11   
 12   # Setup accessible (or protected) attributes for your model
 13   attr_accessible :email, :password, :password_confirmation, :remember_me
 14 end

Project model

  1 class Project < ActiveRecord::Base
  2 
  3   has_many :synapses
  4   has_many :users, :through => :synapses, :dependent => :nullify
  5   has_many :tasks, :dependent => :destroy
  6 
  7   #use this when you don't have callbacks
  8   #has_many :tasks, :dependent => :delete_all
  9 
 10   validates :name, :presence => true,
 11             :uniqueness => true
 12   validates :description, :presence => true
 13 
 14   attr_accessible :name, :description
 15 
 16 end

Synapses model AKA (project members)

  1 class Synapse < ActiveRecord::Base
  2 
  3   belongs_to  :user,
  4               :class_name => 'User',          
  5               :foreign_key => 'user_id'       
  6   belongs_to  :project,
  7               :class_name => 'Project',       
  8               :foreign_key => 'project_id'    
  9   
 10 end

Tasks model

  1 class Task < ActiveRecord::Base
  2 
  3   belongs_to :project
  4   belongs_to :user
  5 
  6   validates :name, :presence => true
  7   validates :description, :presence => true,
  8                           :length => { :minimum => 10 }
  9 
 10   attr_accessible :name, :description
 11 
 12 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-03T08:03:29+00:00Added an answer on June 3, 2026 at 8:03 am

    A callback on the membership model should do it:

    class ProjectMember < ActiveRecord::Base
      after_destroy :remove_project_if_last_member
      private
        def remove_project_if_last_member
          project.destroy if project.users.empty?
        end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an application which has a Projects table and a Users table. In
When I have NSString with /Users/user/Projects/thefile.ext I want to extract thefile with Objective-C methods.
I have a project in mind that will offer users their own domain, like
I have a module that builds an app called MyApp. I have another that
I currently have a routes.rb file that looks like this: map.resources :profiles do |profile|
I have a Company which has Projects within them. As time goes on we
I have a project that uses 3 other projects as libraries and has quite
I have the following routes, which allows for urls like /:username/:project_name resources :users, :path
Question So I've recently came up with some new possible projects that would have
I have a nested resource in my routes.rb like this: map.resources :users, :only =>

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.