I’m passing some json across to a view like so:
@items = Item.where(:custom => false).map do |item|
"{'id': #{item.id}, 'label': '#{item.name}', 'category': '#{item.category}'},"
end
@items = "[#{@items}]"
This works fine locally, with ruby 1.8.7:
[{'id': 1, 'label': 'Ball', 'category', : 'Toy'},{'id': 2, 'label': 'Rat', 'category', : 'Live Rodent'}]
However, upon deploying to heroku (ruby 1.9.2 I believe), horrible things happen:
[["{'id': 1, 'label': 'Ball', 'category', : 'Toy'},", "{'id': 2, 'label': 'Rat', 'category', : 'Live Rodent'},"]];
I’m assuming the difference in ruby version is the issue, but I also doubt that my method is optimal. How can I rewrite this so that it works on both versions?
This will work in Ruby 1.8.7 and 1.9.2:
Your issue is probably due to Ruby 1.9.2 adding an additional way to define hashes, so {key: value} is the same as {:key => value}.