我正在尝试使用类型为hstore数组的postgreSQL列,一切似乎都运行得很好。但是,我的视图会转义数组并将其转换为格式错误的字符串。我的代码看起来像这样:
class AddVariantsToItem < ActiveRecord::Migration
def change
execute 'CREATE EXTENSION hstore'
add_column :items, :variants, :hstore, array: true, default: []
end
end
现在,如果我愿意在 rails console 中使用Item.last.variants
,它会给我一个合适的数组
> Item.last.variants
#=> [{"name"=>"Small", "price"=>"12.00"}, {"name"=>"Medium", "price"=>"20.00"}]
但是,在苗条视图中使用完全相同的代码会给我一个转义字符串:
div
= debug Item.last.variants
/ Gives me:
/ '{"\"name\"=>\"Small\", \"price\"=>\"12.00\"","\"name\"=>\"Medium\", \"price\"=>\"20.00\""}'
使用raw
,==
或.html_save
不会改变任何内容。任何人都可以告诉我,我能做些什么吗?
答案 0 :(得分:1)
Item.last.variants
是一个数组。将某些内容放入视图时,它会被字符串化(我不确定,但我认为它是to_s
方法或inspect
)。
我的建议是你不应该放置整个物体。在这个特定的例子中,我认为你应该迭代它并手动显示数据。