0

I want to create a array and then to insert values for each key (key should be the value from each). But seems to not working. This is my code.

@options = %w(Services Resources)
@images  = []
@options.each do |value|
    @images[value] << Media::Image.where(type: "Media::#{value.singularize}Image")
end

2 Answers 2

2

@images is an array so referencing an element in it should be @images[Integer] and value is a string (in the first iteration it's "Services" and in the second "Resources"). Instead, what would work for you is Hash:

@options = %w(Services Resources)
@images  = {}
@options.each do |value|
    @images[value] = Media::Image.where(type: "Media::#    {value.singularize}Image")
end
Sign up to request clarification or add additional context in comments.

Comments

0

@images is a Array, Array can not use as a Hash. Maybe you want create a Hash like this

@images = Hash.new {|h,k| h[k]=[]}

1 Comment

It should be @images = Hash.new {|h,k| h[k]=[]} if the << operator is used.

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.