1

Due to having to do some fancy searching via cookie storage, I've got an array of (Model) objects.

@ads contains

[{:id => 9283
:name => "Name Here"
:price => 2000
:phone => "999-999-9999"
}]

etc, for multiple objects. Thus, a two-dimensional array (really resembling a Hash, of course, but Rails is dealing with it in Arrays do to the find() and find_all_by() methods being used.

What I need to do, is on a given Ad page, flag a single object as the "current" Ad so it can be denoted as such in the view.

I've isolated it with

@ads.select {|a| a[:id] == id}

But if I attempt

@ads.select {|a| a[:id] == id}.push :current => true

I seem to end up with a new element INSIDE that object element, like so:

@ads then contains

[{:id => 9283
:name => "Name Here"
:price => 2000
:phone => "999-999-9999"},
{:current=>true}]

How can I add it to the existing attributes for the selected object so that it becomes accessible by simply ad[:current] within the view? Should I do a loop?

1 Answer 1

3

select is returning an array, but you want a specific hash in the array, so use find. Now that you're dealing with a hash, you don't need to push, but merge

@ads.find {|a| a[:id] == id}.merge! :current => true
Sign up to request clarification or add additional context in comments.

Comments

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.