Ruby 如何使PostgreSQL和Hstore在Sinatra简单应用程序中工作?

Ruby 如何使PostgreSQL和Hstore在Sinatra简单应用程序中工作?,ruby,postgresql,sinatra,hstore,sinatra-activerecord,Ruby,Postgresql,Sinatra,Hstore,Sinatra Activerecord,我正试图让hstore在Postgre数据库中使用sinatra 1.4.4和sinatra activerecord(1.5.0) 我使用ruby-1.9.3-p392和gems gem 'sinatra' gem 'thin' gem 'sinatra-formhelpers' gem 'pg' gem 'activerecord' gem "sinatra-activerecord" gem 'rake' gem 'activerecord-postgres-hstore' gem 'ac

我正试图让hstore在Postgre数据库中使用sinatra 1.4.4和sinatra activerecord(1.5.0)

我使用ruby-1.9.3-p392和gems

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
以下是我的迁移:


我没有找到我问题的答案。最近的一个在这里,但还不够


有一件事我做错了,但找不到什么。

好的,我在问这个问题后不久就找到了答案

有了新的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

end
需要“活动记录”
需要“activerecord postgres hstore”
类EngineParameter
class 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
require 'active_record'
require 'activerecord-postgres-hstore'

class EngineParameter < ActiveRecord::Base
  serialize :kind, ActiveRecord::Coders::Hstore
end
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'
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

end