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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T14:46:39+00:00 2026-05-28T14:46:39+00:00

I have two entities. User and Role. I am using Devise and CanCan. They

  • 0

I have two entities. User and Role. I am using Devise and CanCan.
They are in a many to many relationship.

User has a lot of roles.
One of the roles is “Administrator”. I verify if my user is an administrator using:

if (user.role? :administrator) .... #this is already implemented and working

I have to validate that never exists more than 2 administrator in the same department on the system. For that purpose I created a custom validate method:

class User < ActiveRecord::Base
    validate :maximum_numbers_of_admins if self.role? :administrator
    belongs_to :department

    def maximum_numbers_of_admins
        #Some code here
    end

In that method I should count the number of Users that have role administrator (without counting myself).
I don’t know how to set the :conditions of my find method to get this number.

This is the specification of the Role class:

# == Schema Information
#
# Table name: roles
#
#  id         :integer         not null, primary key
#  name       :string(255)
#  created_at :datetime
#  updated_at :datetime
#

There is a many to many relationship between users and Roles. (Table roles_users)
Any help with that?

Thanks

  • 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-28T14:46:40+00:00Added an answer on May 28, 2026 at 2:46 pm

    It could be something along this lines:

    def maximum_numbers_of_admins
        if Role.find(:conditions => ['name = ?', 'Administrator']).users.count < 2
          return true
        else
          return false
        end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two entities, a User and Role with a one-to-many relationship from user
Suppose I have two entities: User and UserGroup . They have a one-to-many relationship
I have two entities, Entity1 and Entity2, on a one to many relationship. On
I have two entities: Patient and Job. Patient has a to-many relationship to Job
hi i have two entities User and Authority they have many to many relation:
I have two entities, User and UserSetting. The obvious relationship between these two has
i have two entities one called User and another called Membership which has a
I have two entities Foo and Bar with a Many to Many relationship between
i have two Entities, Parent and Child, that are linked through a bidirectional one-to-many
We have the following two entities with many-to-many association: @Entity public class Role {

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.