activeuuid和rails 4.1.1兼容还是有问题?

时间:2014-05-16 16:29:18

标签: activerecord rubygems uuid ruby-on-rails-4.1

以下是我尝试用来实现gem activeuuid的一小段代码:

的Gemfile

source 'https://rubygems.org'
gem 'rails', '4.1.1'
gem 'pg'
gem 'sass-rails', '~> 4.0.3'
gem 'uglifier', '>= 1.3.0'
gem 'coffee-rails', '~> 4.0.0'
gem 'jquery-rails'
gem 'turbolinks'
gem 'jbuilder', '~> 2.0'
gem 'sdoc', '~> 0.4.0',          group: :doc
gem 'spring',        group: :development
gem 'activeuuid'

User.rb

class User < ActiveRecord::Base
  include ActiveUUID::UUID
end

迁移 xxxxxxxxx_create_users.rb

class CreateUsers < ActiveRecord::Migration
  def self.up
    create_table :users, :id => false  do |t|
      t.uuid :id, :primary_key => true
      t.string :email

      t.timestamps
    end
  end

  def self.down
    drop_table :users
  end

end

即使在捆绑或重新启动控制台之后,一个简单的查询也不会被证明是可见的。

 2.1.1 :016 > User.all
    NoMethodError: undefined method `set_primary_key' for User (call 'User.connection' to establish a connection):Class
        from /home/sahil/.rvm/gems/ruby-2.1.1@myapp/gems/activerecord-4.1.1/lib/active_record/dynamic_matchers.rb:26:in `method_missing'
        from /home/sahil/.rvm/gems/ruby-2.1.1@myapp/gems/activeuuid-ps-0.1.2/lib/activeuuid/uuid.rb:54:in `block in <module:UUID>'
        from /home/sahil/.rvm/gems/ruby-2.1.1@myapp/gems/activesupport-4.1.1/lib/active_support/concern.rb:120:in `class_eval'
        from /home/sahil/.rvm/gems/ruby-2.1.1@myapp/gems/activesupport-4.1.1/lib/active_support/concern.rb:120:in `append_features'
        from /home/sahil/projects/test_projects/myapp/app/models/user.rb:3:in `include'
        from /home/sahil/projects/test_projects/myapp/app/models/user.rb:3:in `<class:User>'
        from /home/sahil/projects/test_projects/myapp/app/models/user.rb:1:in `<top (required)>'
        from /home/sahil/.rvm/gems/ruby-2.1.1@myapp/gems/activesupport-4.1.1/lib/active_support/dependencies.rb:443:in `load'
        from /home/sahil/.rvm/gems/ruby-2.1.1@myapp/gems/activesupport-4.1.1/lib/active_support/dependencies.rb:443:in `block in load_file'
        from /home/sahil/.rvm/gems/ruby-2.1.1@myapp/gems/activesupport-4.1.1/lib/active_support/dependencies.rb:633:in `new_constants_in'
        from /home/sahil/.rvm/gems/ruby-2.1.1@myapp/gems/activesupport-4.1.1/lib/active_support/dependencies.rb:442:in `load_file'
        from /home/sahil/.rvm/gems/ruby-2.1.1@myapp/gems/activesupport-4.1.1/lib/active_support/dependencies.rb:342:in `require_or_load'
        from /home/sahil/.rvm/gems/ruby-2.1.1@myapp/gems/activesupport-4.1.1/lib/active_support/dependencies.rb:480:in `load_missing_constant'
        from /home/sahil/.rvm/gems/ruby-2.1.1@myapp/gems/activesupport-4.1.1/lib/active_support/dependencies.rb:180:in `const_missing'
        from (irb):16
        from /home/sahil/.rvm/gems/ruby-2.1.1@myapp/gems/railties-4.1.1/lib/rails/commands/console.rb:90:in `start'
        from /home/sahil/.rvm/gems/ruby-2.1.1@myapp/gems/railties-4.1.1/lib/rails/commands/console.rb:9:in `start'
        from /home/sahil/.rvm/gems/ruby-2.1.1@myapp/gems/railties-4.1.1/lib/rails/commands/commands_tasks.rb:69:in `console'
        from /home/sahil/.rvm/gems/ruby-2.1.1@myapp/gems/railties-4.1.1/lib/rails/commands/commands_tasks.rb:40:in `run_command!'
        from /home/sahil/.rvm/gems/ruby-2.1.1@myapp/gems/railties-4.1.1/lib/rails/commands.rb:17:in `<top (required)>'
        from bin/rails:4:in `require'
        from bin/rails:4:in `<main>'

甚至

2.1.1 :029 >   User.count
    NoMethodError: undefined method `set_primary_key' for User (call 'User.connection' to establish a connection):Class
        from /home/sahil/.rvm/gems/ruby-2.1.1@myapp/gems/activerecord-4.1.1/lib/active_record/dynamic_matchers.rb:26:in `method_missing'
        from /home/sahil/.rvm/gems/ruby-2.1.1@myapp/gems/activeuuid-ps-0.1.2/lib/activeuuid/uuid.rb:54:in `block in <module:UUID>'
        ..........
        ..........
        from bin/rails:4:in `require'
        from bin/rails:4:in `<main>'

这里发生了什么错误。我错过了一些必要的依赖。

1 个答案:

答案 0 :(得分:0)

找到一个更好的解决方案,并按以下方式实施:http://www.codesapling.com/blog/2014/05/24/using-uuid-as-primary-key-in-rails4-with-postgres/