我建立了我的Rails应用程序3.2.x中使用PostgreSQL的HStore但我发现了一个错误。 它看起来像hstore扩展还没有被环境拾起。 我已经重新启动我的机器,检查数据库扩展等。
当我尝试执行:
User.where("settings @> (:key => :value)", :key => "setting_x", :value => "test")
我得到一个错误:( @>
不被识别,即扩展hstore
?未安装)
HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts.
我的Rails应用程序的设置是:
Gemfile.rb:
gem 'activerecord-postgres-hstore'
移民:
add_column :users, :settings, :hstore
execute "CREATE INDEX CONCURRENTLY users_gin_settings ON users USING GIN(settings)"
# can see the extenstion installed on my local dev psql database after this
User
模式:
serialize :settings, ActiveRecord::Coders::Hstore
动态User
的方法:
# metaprogramming: has_setting_x + instance.setting_x
%w[setting_x setting_y setting_z].each do |key|
attr_accessible key
# doesn't work > throws error because of the @> operator
scope "has_#{key}", lambda { |value| where("settings @> (? => ?)", key, value) }
# works: can use instance.setting_x
define_method(key) do
settings && settings[key]
end
# works: can use instance.setting_x = "value"
define_method("#{key}=") do |value|
self.settings = (settings || {}).merge(key => value)
end
end
更新1:
这工作时,我直接对话到PostgreSQL数据库:
SELECT "users".* FROM "users" WHERE (settings @> hstore('setting_x','6D9Q7RO4SVWHXK86F'));
该Hstore文档说:
The => operator is deprecated and may be removed in a future release. Use the hstore(text, text) function instead.
所以,从它的模样,我的PostgreSQL数据库版本(9.2.1)已经过时了=>
符号。 它看起来像我有更多的研究遥遥领先。