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

  • Home
  • SEARCH
  • 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 6321861
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T16:13:31+00:00 2026-05-24T16:13:31+00:00

I have got this model: class User < ActiveRecord::Base has_and_belongs_to_many :roles before_destroy do |u|

  • 0

I have got this model:

class User < ActiveRecord::Base
  has_and_belongs_to_many :roles

  before_destroy do |u|
    if u.superadmin? and User.joins(:roles).where(:roles => {:superadmin => true}).count == 1
      u.errors.add(:base, "cannot delete last admin user")
      return false
    end
    return true
  end
end

class Role < ActiveRecord::Base
  has_and_belongs_to_many :users
end

When I try to delete last superadmin, the before_destroy callback adds to errors array and return false. I get the error message in the controller, everything seems to be fine.

Except Rails3 deletes me record from the join table. Why? I returned false, as I return false it should not proceed with the deletion. Here is the log:

SQL (0.2ms)  SELECT COUNT(*) FROM "users" INNER JOIN "roles_users" ON "roles_users"."user_id" = "users"."id" INNER JOIN "roles" ON "roles"."id" = "roles_users"."role_id" WHERE "roles"."superadmin" = 't'
AREL (0.4ms)  DELETE FROM "roles_users" WHERE "roles_users"."user_id" = 1 AND "roles_users"."role_id" IN (1, 4)
Organization Load (0.4ms)  SELECT * FROM "organizations" INNER JOIN "organizations_users" ON "organizations".id = "organizations_users".organization_id WHERE ("organizations_users".user_id = 1 )

Thanks for help.

  • 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-05-24T16:13:32+00:00Added an answer on May 24, 2026 at 4:13 pm

    This seems like this rails error, if you use older Rails version (it should be solved in Rails 3.0.6 AFAIK):

    https://rails.lighthouseapp.com/projects/8994/tickets/6191-habtm-association-is-being-destroyed-before-the-before_destory-callbacks-are-executed

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got the following User model: class User < ActiveRecord::Base # Users table has
I've got some models set up like this: class AppGroup(models.Model): users = models.ManyToManyField(User) class
I've got a sqlalchemy model that is set up like this: class Entry(Base): __tablename__
I have the following models: class Product < ActiveRecord::Base belongs_to :brand belongs_to :model accepts_nested_attributes_for
I have the following models: class User < ActiveRecord::Base has_many :books, :dependent => :destroy
I have a ActiveRecord Car model: class Car < ActiveRecord::Base def check_value puts self.load_size
Let's say I've defined this model: class Identifier(models.Model): user = models.ForeignKey(User) key = models.CharField(max_length=64)
I've got an easy entity model Bank 1---∞ User 1---∞ Session In partial class
I have got this error when using Enterprise Library 3.1 May 2007 version. We
I have got this code: XDocument xdoc = XDocument.Load(URI); XElement root = xdoc.Element(forecast); //get

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.