0

There's a good number of related questions but their answers haven't helped me. I have a method fetch_all_sections which populates an array with this line:

all_sections << {:id => section.id, :sortlabel => section.sortlabel, :title => section.title, :depth => depth}

In a loop in a view, I would like to easily access the values by their key, like this:

<% fetch_all_sections(@standard).each do |section| %>
  <%= section.id %>
<% end %>

This says no method id on section. section[:id] and #{section['id']} have similarly themed errors. I used a hash for ease of retrieval - should I use a different structure?

I'm hoping I don't need .map like section.map { |id| id[:id] } for every value.

EDIT: Here's the context. It's a little loopy (pun intended) but it does what's intended.

# Calls itself for each section recursively to fetch all possible children
def fetch_all_sections(standard, section = nil, depth = 0)
  all_sections = []

  if section.nil?
    rootsections = standard.sections.sorted
    if ! rootsections.nil?
      rootsections.each_with_index do |section, i|
        depth = section.sortlabel.split('.').length - 1
        all_sections.push(fetch_all_sections(standard, section, depth))
      end
    end
  else

    all_sections << {:id => section.id, :sortlabel => section.sortlabel, :title => section.title, :depth => depth}

    section.children.sorted.each do |section|
      all_sections | fetch_all_sections(standard, section)
    end
  end

  return all_sections
end

1 Answer 1

1

Try with the following:

<% fetch_all_sections(@standard).each do |section| %>
  <%= section['id'] %>
<% end %>

If not working, try debugging using these methods:

<% fetch_all_sections(@standard).each do |section| %>
  <%= section.inspect %>
  <%= section.class %>
<% end %>

As the Question author said, this fixed:

all_sections << fetch_all_sections(standard, section, depth).first

And tell us the output of the inspect

Sign up to request clarification or add additional context in comments.

8 Comments

It indeed seems that I've made an array of arrays with 1 hash that has multiple key, value pairs. This works section[0][:id]. flatten apparently doesn't affect it.
Can you post the code of your fech_all_sections method please @Archonic?
Perhaps since the method calls itself, the array initializer is being pushed.. to itself? Inception! But all_sections = [] if all_sections.nil? changed nothing.
fetch_all_sections returns an array and you push the result of fetch_all_sections into another Array. You have a bi-dimensionnal Array here, you need to flatten it after your push.
flatten unfortunately doesn't remove sub-arrays if each element is by itself an array. [[1],[2],[3]].flatten #=> [[1],[2],[3]]
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.