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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T17:56:22+00:00 2026-05-22T17:56:22+00:00

I have a model User which goes like this class User ROLES = {

  • 0

I have a model User which goes like this

class User

  ROLES = {
            "Admin" => 1,
            "Manager" => 2,
            "Officer" => 3
          }

  def admin?
    role == 1
  end

  def manager?
    role == 2
  end

  def officer?
    role == 3
  end

end

where role is a column in users table and I was wondering whether I can use a common method to build the 3 role checking methods from the ROLES hash?

Edit

so this seems to be the answer

class User

  ROLES = {
            "Admin" => 1,
            "Manager" => 2,
            "Officer" => 3
          }

  # methods used to identify whether a user is a specific role
  ROLES.each { |k, v| define_method("#{k.downcase}?") { role == v } }

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-05-22T17:56:23+00:00Added an answer on May 22, 2026 at 5:56 pm

    I am not fully sure of your intention, but maybe like this?

    class User
      %w[admin manager officer].each_with_index{|m, i| define_method("#{m}?"){role == i+1}}
    end
    

    or, using ROLES,

    class User
      ROLES.each{|m, i| define_method("#{m.downcase}?"){role == i}}
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a model Foo which have a ForeignKey to the User model. Later,
So I have a User model that :has_many other models like Documents, Videos, Posts
I have a data model that includes common columns like addedBy, editedby (user), addedDate,
I have a user model and an operator model which belongs to user. I
I have a model which has a template field. This template is HTML and
For example, if I have a user model and I need to validate login
In my user model, I have an attribute called nickname and validates as such:
Suppose you have two models, User and City, joined by a third model CityPermission:
I have a web user control which call some methods of an interface. Four
I have a web application which will return a user id based on the

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.