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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T23:58:55+00:00 2026-06-02T23:58:55+00:00

I’m working on a CRUD interface for managing users in my application. I have

  • 0

I’m working on a CRUD interface for managing users in my application. I have a constant User::ROLES that is an array with the names of valid user roles in my app (admin, teacher, student).

What I’m wanting to do is, in the index action of the controller, have a block that iterates through the ROLES constant and creates a scoped instance variable from @users (which is already initialized before the block). Here’s what the index method looks like so far:

def index
    @users = user.all
    #@students = @users.where(:role => "student") # This works by itself
    User::ROLES.each do |r|
        @r = @users.where(:role => r.to_s)
    end
end

So I want to be able to name the instance variable by what’s passed through the block, so @r creates @admin, @teacher, etc. Since I’m new to ruby and rails, I don’t quite understand the syntax for doing this. Who knows? There’s probably a better way to do it anyways.

EDIT

Just to let everyone know, I’m hoping to use this in the view on the index action to display a list of users grouped by their role. Of course, it will be used in other ways too throughout the CRUD interface, which is why I didn’t clarify the use-case earlier, since it’s multi-purpose.

  • 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-02T23:58:56+00:00Added an answer on June 2, 2026 at 11:58 pm

    This is probably a bad idea. If you change a role in your model, you will need to update your view accordingly to use a different instance method. A better way to handle this would be to simply group users by their role:

    @users = User.group(:role).all
    

    Which would give you a hash with keys named after each role that you could easily use to build a dynamic view:

    <% @users.each_pair do |role, users| %>
      <h2><%= role.to_s.titlelize %></h2>
      <% users.each do |user| %>
        ...
      <% end %>
    <% end %>
    

    You can also access users with a specific role if needed:

    <h2>Admin users:</h2>
    <%= @users[:admin].map(&:name).to_sentence %>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace

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.