未初始化的常量ActiveRecord :: Coders :: Hstore(NameError)[带PG / Rails 3.2.11的hstore]

时间:2013-06-24 19:17:42

标签: ruby-on-rails hstore

我有一个Rails 3.2.11应用程序,我尝试使用activerecord-postgres-hstore gem添加Postgres Hstore。该应用程序可在PG-9.1.9 db开发中查找,但在PG-9.1.8 db的生产服务器中出现以下错误:uninitialized constant ActiveRecord::Coders::Hstore (NameError)

日志文件如下:

unicorn.log

E, [2013-06-24T18:48:27.379430 #901] ERROR -- : reaped #<Process::Status: pid 16706 exit 1> worker=1
I, [2013-06-24T18:48:27.379703 #901]  INFO -- : worker=1 spawning...
I, [2013-06-24T18:48:27.385576 #16719]  INFO -- : worker=1 spawned pid=16719
I, [2013-06-24T18:48:27.385854 #16719]  INFO -- : Refreshing Gem list
E, [2013-06-24T18:48:27.391198 #901] ERROR -- : reaped #<Process::Status: pid 16708 exit 1> worker=0
I, [2013-06-24T18:48:27.391379 #901]  INFO -- : worker=0 spawning...
I, [2013-06-24T18:48:27.392775 #16722]  INFO -- : worker=0 spawned pid=16722
I, [2013-06-24T18:48:27.392991 #16722]  INFO -- : Refreshing Gem list
E, [2013-06-24T18:48:29.235072 #16712] ERROR -- : uninitialized constant ActiveRecord::Coders::Hstore (NameError)
/home/slaxman/apps/itextbook/releases/20130624183910/app/models/book.rb:7:in `<class:Book>'
/home/slaxman/apps/itextbook/releases/20130624183910/app/models/book.rb:5:in `<top (required)>'
/home/slaxman/apps/itextbook/releases/20130624183910/app/admin/books.rb:1:in `<top (required)>'

production.log

Connecting to database specified by database.yml

book.rb

class Book < ActiveRecord::Base

    serialize :properties , ActiveRecord::Coders::Hstore
    serialize :bookmark_count , ActiveRecord::Coders::Hstore

    attr_accessible :title, :content, :input_method, :bookmarks_attributes, :bookmark_count, :properties


    has_many :bookmarks
    accepts_nested_attributes_for :bookmarks, allow_destroy: true
end

的Gemfile

source 'https://rubygems.org'

gem 'rails', '3.2.11'
# Bundle edge Rails instead:
# gem 'rails', :git => 'git://github.com/rails/rails.git'

group :production do
        gem 'pg', '0.14.1'
        gem 'therubyracer','0.11.4'
        gem 'execjs', '1.4.0'
end


# Gems used only for assets and not required
# in production environments by default.
group :assets do
        gem 'sass-rails',   '~> 3.2.6'
        gem 'jquery-rails', '~> 2.2.1'
        gem 'coffee-rails', '~> 3.2.2'
        gem 'uglifier', '~> 1.3.0'
        gem 'bootstrap-sass', '~> 2.3.1.2'
        gem 'bootswatch-rails', '~> 0.5.0'
end

gem 'unicorn', '~> 4.6.2'
gem 'capistrano', '~> 2.14.2'
gem "multi_json", "~> 1.2.0"
gem 'devise' , '~> 2.2.3'
gem 'cancan', '~> 1.6.8'
gem 'kaminari', '~> 0.14.1'
gem 'kramdown', '~> 0.14.1'
gem 'nokogiri', '~> 1.5.6'
gem 'colorbox-rails', '~> 0.0.9'
gem 'coderay', '~> 1.0.8'
gem 'mercury-rails', '~> 0.9.0'
gem 'carrierwave', '~> 0.8.0'
gem 'fog', '~> 1.9.0'
gem 'activeadmin', '~> 0.5.1'
gem 'meta_search', '~> 1.1.3'
gem 'pg_hstore', '~> 0.0.1'
gem "activerecord-postgres-hstore", "~> 0.7.6"

谢谢!

2 个答案:

答案 0 :(得分:0)

事实证明解决方案非常简单。我将hstore初始化为一个空数组,问题解决了。

<强> book.rb

serialize :bookmark_count , ActiveRecord::Coders::Hstore.new({})
serialize :bookmark_count , ActiveRecord``::Coders::Hstore.new({})

答案 1 :(得分:0)

Rails 4及以上

如果您使用的是Rails 4,则不需要activerecord-postgres-hstore gem,因为ActiveRecord 4提供HStore类型支持

更改

class Book < ActiveRecord::Base
  serialize :properties , ActiveRecord::Coders::Hstore
  serialize :bookmark_count , ActiveRecord::Coders::Hstore
end

class Book < ActiveRecord::Base
  # You don't need it anymore
  # serialize :properties , ActiveRecord::Coders::Hstore
  # serialize :bookmark_count , ActiveRecord::Coders::Hstore
end

阅读here