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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T02:49:12+00:00 2026-06-07T02:49:12+00:00

I have a hash of values that I am trying to loop through to

  • 0

I have a hash of values that I am trying to loop through to display the values in my view.

The hash is set up as:

{:position=>#<Position id: 2, user_id: 1, title: "Something", city: "Denver", state: "CO",   
created_at: "2012-07-06 02:55:42", updated_at: "2012-07-06 02:55:42">, :experience=>[#
<Experience id: 4, user_id: 1, position_id: 2, description: "Did some stuff", created_at: 
"2012-07-02 06:24:33", updated_at: "2012-07-02 06:24:33">, #<Experience id: 6, user_id: 1, 
position_id: 2, description: "Did other stuff", created_at: "2012-07-02 06:24:33", 
updated_at: "2012-07-02 06:24:33">]}

It is created by taking ActiveRecord results and inserting them into a hash (if you want more details, I am happy to add them).

In my view, I attempt to loop through the hash:

<% for i in 0..@positions.length %>
  <%= @positions[i][:position][:title] %>
  <%= @positions[i][:position][:city] %>
<% end %>

When I open it in the browser, I get an error undefined method '[]' for nil:NilClass. If I only use @positions[i], it will dump out the raw hash (first one, then second, and so on). It’s when I add [:position], it doesn’t work. I can access the values in the console using @positions[0][:position][:title]. When I tried writing the loop in console, I got the same error.

I know that i is counting because I can have it display the value in the browser and it is working correctly.

I have tried using @positions.each do |job| but ran into the same error. I tried adding another indices in other places: @positions[i][:position][0].

Basically, I have two models: Position and Experience. Experience is a child to Position. I want to get Experience for a given Position but only certain experience like ones that occur within a certain time frame. There is most likely a better approach to this and I just don’t know it so I’m open to suggestions.

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-06-07T02:49:15+00:00Added an answer on June 7, 2026 at 2:49 am

    It seems you have an object of Postion class assigned to the key :position. So you access title and city through accessor methods and not with [].

    <% @positions.each do |job| %>
      <%= job[:position].title %>
      <%= job[:position].city %>
    <% end %>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a python program that takes the md5 & sha1 hash values of
I have been have issues trying to retrieve the values from a treeMap that
I have an initialize method that accepts a hash to set some instance variables.
I have some code as follows. I am trying to search through the hash
I'm trying to have a (hash-based) Multimap with a (hash-based) Multiset of values for
I have a CSV that I like to save all my hash values on
I have written a simple Java class to generate the hash values of the
I have a Hash Map (many-to-one relationship between texts and boolean values): name flag
How do I swap keys and values in a Hash? I have the following
Why two object's hash code is not same even though they have similar values.

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.