Web*_*ion 1 ruby ruby-on-rails heroku postgresql-9.1 hstore
我设置我的Rails 3.2.x应用程序以使用PostgreSQL HStore,但我收到一个错误.看起来环境没有拾取hstore扩展.我已经重新启动了我的机器,检查了数据库扩展等.
当我尝试执行时:
User.where("settings @> (:key => :value)", :key => "setting_x", :value => "test")
Run Code Online (Sandbox Code Playgroud)
我收到一个错误:( @>无法识别,即hstore没有安装扩展程序?)
HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts.
Run Code Online (Sandbox Code Playgroud)
我的Rails应用程序设置是:
Gemfile.rb:
gem 'activerecord-postgres-hstore'
Run Code Online (Sandbox Code Playgroud)
移民:
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
Run Code Online (Sandbox Code Playgroud)
User 模型:
serialize :settings, ActiveRecord::Coders::Hstore
Run Code Online (Sandbox Code Playgroud)
动态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
Run Code Online (Sandbox Code Playgroud)
更新1:
当我直接与PostgreSQL DB交谈时,这是有效的:
SELECT "users".* FROM "users" WHERE (settings @> hstore('setting_x','6D9Q7RO4SVWHXK86F'));
Run Code Online (Sandbox Code Playgroud)
该Hstore文档说:
The => operator is deprecated and may be removed in a future release. Use the hstore(text, text) function instead.
Run Code Online (Sandbox Code Playgroud)
因此,从它的外观来看,我的PostgreSQL数据库版本(9.2.1)已经弃用了该=>表示法.看起来我还有更多的研究.
来自PostgreSQL文档:
该
=>运算符已弃用,可能会在将来的版本中删除.请改用此hstore(text, text)功能.
所以从我看来,我的PG数据库版本(9.2.1)已经弃用了该=>表示法.
现在在本地和Heroku上工作.
例:
User.where("settings @> hstore(?,?)", "setting_x", key)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2442 次 |
| 最近记录: |