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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T01:41:42+00:00 2026-05-18T01:41:42+00:00

I have two ActiveRecord classes. A simplified view of these classes: class Account <

  • 0

I have two ActiveRecord classes. A simplified view of these classes:

class Account < ActiveRecord::Base
  has_many :user_account_roles
end

class UserAccountRole < ActiveRecord::Base
  belongs_to :account

  # Has a boolean attribute called 'administrator'.
end

What I’m struggling with is that I’d like to be able to apply two validation rules to this:
* Ensuring that the last UserAccountRole cannot be removed.
* Ensuring that the last UserAccountRole that is an administrator cannot be removed.

I’m really struggling to understand the best way of achieving this kind of structural validation. I’ve tried adding a before_remove callback to the association, but I don’t like that this has to throw an error which would need to be caught by the controller. I’d rather this be treated as ‘just another validation’.

class Account < ActiveRecord::Base
  has_many :user_account_roles, :before_remove => check_remove_role_ok

  def check_remove_relationship_ok(relationship)
    if self.user_account_relationships.size == 1
      errors[:base] << "Cannot remove the last user from this account."
      raise RuntimeError, "Cannot remove the last user from this account."
    end
  end

end

I don’t think this makes any difference, but I’m also using accepts_nested_attributes_for.

  • 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-18T01:41:43+00:00Added an answer on May 18, 2026 at 1:41 am

    Why not use a simple validation on Account?

    class Account < ActiveRecord::Base
      has_many :user_account_roles
    
      validate :at_least_one_user_account_role
      validate :at_least_one_administrator_role
    
      private
      def at_least_one_user_account_role
        if user_account_roles.size < 1
          errors.add_to_base('At least one role must be assigned.')
        end
      end
    
      def at_least_one_administrator_role
        if user_account_roles.none?(&:administrator?)
          errors.add_to_base('At least one administrator role must be assigned.')
        end
      end
    end
    

    This way nothing needs to be raised, and the record won’t be saved unless there’s at least one role, and at least one administrator role. Thus when you re-render your edit form on error, this message will show up.

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

Sidebar

Related Questions

I have two classes: class User < ActiveRecord::Base :has_one :foo end class Foo <
I have two models class User < ActiveRecord::Base has_many :requests, :foreign_key => :recipient_id .
I have two ActiveRecord classes class Product : ActiveRecordBase<Product> { private IList<Usage> _usages =
I have two types of classes: BaseUser < ActiveRecord::Base and User < BaseUser which
I have two connected classes defined like this: [ActiveRecord] public class Store : ActiveRecordBase<Store>
I have two classes, and want to include a static instance of one class
I have two classes that I would like to specify as follows: class Club
How do I model the following using Castle ActiveRecord? I have two classes, Customer
I have a basic ActiveRecord model in which i have two fields that i
I have two classes and I want to establish a many-to-many assications, here is

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.