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 [email protected] %>
<%= @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.