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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T06:53:35+00:00 2026-05-23T06:53:35+00:00

I am using Ruby on Rails 3.0.7 and I would like to understand how

  • 0

I am using Ruby on Rails 3.0.7 and I would like to understand how to handle the following code in order to retrieve a class objects with a specified id.

In my view file I have:

@records = Users.all # This returns an array (class)

In another file, a partial template, I would like to retrieve, for example, the user with id 1, but if I make this:

@records.find(1)

I get an enumerator (class) of all records:

<Enumerator: [<Users id: 1, ... ] >

How can I find the user with id 1 (or other ids) “a là Ruby on Rails Way”?


UPDATE

I use @records = Users.all in a view file because I aim to minimize calls to the database since I need to iterate almost over all records and check them existence. If I do for example:

some_hash.each { |key, value|
  put User.find(value)
}

and I go in the log file, I will see a lot of database requests.

  • 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-23T06:53:35+00:00Added an answer on May 23, 2026 at 6:53 am

    Even though this is probably quite slow, and I suspect there are some less than optimal designs in the app you’re working on (not judging, we’ve all been there), Array#index seems to be what you’re looking for:

    @records[@records.index{|user| user.id == 1}]
    

    Edit

    Although if you need to do something for every user, and you need to access them by id quickly, I’d probably do something like this in your controller. Even if it’s not really faster, it’s much more readable (to me anyways):

    @users_hash = {}
    User.all.each{|user| @users_hash[user.id] = user}
    

    Then in your views you can do:

    @users_hash[id].username
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to create something like this using ruby on rails. Im not
I am using Ruby on Rails 3 and I would like to use jQuery
I am using Ruby on Rails 3 and I would like to change its
I'm using Ruby on Rails (Edge, the development version), and Ruby rvm 1.9.2. application.js
I have a Ruby on Rails application that has two active environments, Stage and
Greetings, all, I'm working on an application in Ruby on Rails where we need
I'm starting to learn Rails. I'm using Rails 3. In my simple App, I
I am trying to attach files in my RoR app. I am using Rails
I would like to allow only mobile devices on my web site, such as
I would appreciate some advice on how to structure a database for the following

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.