Reputation: 1348
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?
Upvotes: 1
Views: 1895
Reputation: 4023
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
Upvotes: 3