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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T00:12:37+00:00 2026-06-11T00:12:37+00:00

I have a User model with attributes department and points. I have grouped the

  • 0

I have a User model with attributes department and points. I have grouped the users by department and sorted each department by the number of points they contain, as shown below in my home/index view:

Accounting 158
Animal Science 98
Kinesiology 58 

I simply want to get the index of each hash element in the array so I can do something like this:

1. Accounting 158
2. Animal Science 98
3. Kinesiology 58 

This is the code in my home_controller:

class HomeController < ApplicationController  
def index
@users = User.find(:all)
@dep_users = @users.group_by { |u| u.department}
end

And in my home/index view I have this code:

    <% @dep_users.sort.each do |department, users| %>
    <% @p = Array.new() %>

    <%= department %>
    <% for user in users %>
    <% @p << user.points %>
    <% end %>
    <%= @p.inject(:+) %>
    <% end %>
    <% end %>

I have tried using each_with_index on @dep_users as such:

@dep_users.sort.each_with_index do |department, users, index|

but I keep getting this error:

undefined method 'each' for 0:FixNum when I do that

How could I get the index for each hash element in the array?

  • 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-11T00:12:39+00:00Added an answer on June 11, 2026 at 12:12 am

    I think, it’s a bad idea to move so much logic in your views. Better way is to define new method which will return sorted array [[department1, points1], [department2, points2]] in your model. Then call it from your controller to create an instance variable. And this variable in your view with code like this

    <ol>
      <% @p.each do |item| %>
        <li><%= item[0] + '&nbsp;' + item[1] %></li>
      <% end %>
    </ol>
    

    Let html puts numbers before each item of your list.

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

Sidebar

Related Questions

In my app, I have a User model, which includes a number of attributes
I'd like to have a number of dynamic attributes for a User model, e.g.,
I have a User model and a Submission model. Each Submission has a ForeignKey
I have a user model with attributes 'first' and 'last' So for example User.first.first
I have a User model with the usual attributes such as email and hashed_password
I have a model User where I make heavy use of dynamic attributes. When
I have a User model with a number of very similar properties that I
I've got a question about updating attributes. I have an user model and also
Hey guys, i have 5 model attributes, for example, 'str' and 'dex'. A user
I have a model called User , and another called Run . Each user

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.