我试图在Postgre数据库中使用sinatra 1.4.4和sinatra-activerecord (1.5.0)。
我和ruby-1.9.3-p 392一起工作
gem 'sinatra'
gem 'thin'
gem 'sinatra-formhelpers'
gem 'pg'
gem 'activerecord'
gem "sinatra-activerecord"
gem 'rake'
gem 'activerecord-postgres-hstore'
gem 'activesupport'
group :development, :test do
gem 'rspec'
gem 'capybara'
gem 'pry'
end这些是我的迁徙:
class AddHstore < ActiveRecord::Migration
def up
execute 'CREATE EXTENSION hstore'
end
def down
execute 'DROP EXTENSION hstore'
end
endclass CreateEngineParameters < ActiveRecord::Migration
def self.up
create_table :engine_parameters do |t|
t.hstore :kind
t.timestamps
end
end
def self.down
drop_table :engine_parameters
end
end使用模型engine_parameter.rb中的这些行:
require 'active_record'
require 'activerecord-postgres-hstore'
class EngineParameter < ActiveRecord::Base
serialize :kind, ActiveRecord::Coders::Hstore
end然后,在控制台中,我想要创建一个带有hstore的对象:
a = EngineParameter.new
=> #<EngineParameter id: nil, kind: {}, created_at: nil, updated_at: nil>
a.kind['test'] = "test"
=> "test"
a
=> #<EngineParameter id: nil, kind: {"test"=>"test"}, created_at: nil, updated_at: nil>
a.save
NoMethodError: undefined method `scan' for {"test"=>"test"}:Hash
from /Users/mycomputername/.rvm/gems/ruby-1.9.3-p392@myproject/gems/pg-hstore-1.2.0/lib/pg_hstore.rb:24:in `load'然后我又试了一次:
a.save
NoMethodError: undefined method `map' for "\"test\"=>\"test\"":String
from /Users/mycomputername/.rvm/gems/ruby-1.9.3-p392@myproject/gems/pg-hstore-1.2.0/lib/pg_hstore.rb:49:in `dump'我没有找到我问题的答案。离这里最近的是:https://gist.github.com/bru/3258069,但还不够。
有件事我做错了,但找不到什么。
发布于 2015-03-19 16:08:56
好的,我问了这个问题后不久就找到了答案。
有了新的activerecord,即使是在sinatra,你只要把这个
require 'active_record'
require 'activerecord-postgres-hstore'
class EngineParameter < ActiveRecord::Base
# don't do this
# serialize :kind, ActiveRecord::Coders::Hstore
# do this
store_accessor :kind
endhttps://stackoverflow.com/questions/22689000
复制相似问题