'add key=value to a hash inside Rails without having to modify Model
I have an array of objects (hash) and I want to add a new key to the list of objects, iterating with .each method on it.
myArray = [];
@VARIABLE.each_with_index do |ITEM, index|
myArray << ITEM
myArray[:key]="value"
end
but Rails keeps returning me the error can't write unknown attribute key
I don't wanna modify Rails Model structure... I'm basically creating a helper array of objects to loop them in a JS React component in the view...
(JS dev here)
Solution 1:[1]
obj.attributes.merge({key:value}) solved the problem.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | jeanm |
