activeuuid: Error with PostgreSQL

Rails 4.2 Ruby 2.2.0

The error happens when I execute Model.create. With SQLite works fine.

TypeError: can't cast UUIDTools::UUID to uuid
    from /opt/code/lista_compras_server/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.0/lib/active_record/connection_adapters/abstract/quoting.rb:34:in `rescue in type_cast'
    from /opt/code/lista_compras_server/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.0/lib/active_record/connection_adapters/abstract/quoting.rb:23:in `type_cast'
    from /opt/code/lista_compras_server/shared/bundle/ruby/2.2.0/gems/activeuuid-0.6.0/lib/activeuuid/patches.rb:128:in `type_cast_with_visiting'
    from /opt/code/lista_compras_server/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.0/lib/active_record/connection_adapters/postgresql_adapter.rb:598:in `block in exec_cache'
    from /opt/code/lista_compras_server/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.0/lib/active_record/connection_adapters/postgresql_adapter.rb:597:in `map'
    from /opt/code/lista_compras_server/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.0/lib/active_record/connection_adapters/postgresql_adapter.rb:597:in `exec_cache'
    from /opt/code/lista_compras_server/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.0/lib/active_record/connection_adapters/postgresql_adapter.rb:585:in `execute_and_clear'
    from /opt/code/lista_compras_server/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.0/lib/active_record/connection_adapters/postgresql/database_statements.rb:160:in `exec_query'
    from /opt/code/lista_compras_server/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.0/lib/active_record/connection_adapters/postgresql/database_statements.rb:192:in `exec_insert'
    from /opt/code/lista_compras_server/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.0/lib/active_record/connection_adapters/abstract/database_statements.rb:108:in `insert'
    from /opt/code/lista_compras_server/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.0/lib/active_record/connection_adapters/abstract/query_cache.rb:14:in `insert'
    from /opt/code/lista_compras_server/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.0/lib/active_record/relation.rb:64:in `insert'
    from /opt/code/lista_compras_server/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.0/lib/active_record/persistence.rb:521:in `_create_record'
    from /opt/code/lista_compras_server/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.0/lib/active_record/counter_cache.rb:139:in `_create_record'
    from /opt/code/lista_compras_server/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.0/lib/active_record/attribute_methods/dirty.rb:127:in `_create_record'
    from /opt/code/lista_compras_server/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.0/lib/active_record/callbacks.rb:306:in `block in _create_record'
... 20 levels...
    from /opt/code/lista_compras_server/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.0/lib/active_record/connection_adapters/abstract/transaction.rb:188:in `within_new_transaction'
    from /opt/code/lista_compras_server/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.0/lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `transaction'
    from /opt/code/lista_compras_server/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.0/lib/active_record/transactions.rb:220:in `transaction'
    from /opt/code/lista_compras_server/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.0/lib/active_record/transactions.rb:344:in `with_transaction_returning_status'
    from /opt/code/lista_compras_server/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.0/lib/active_record/transactions.rb:286:in `block in save'
    from /opt/code/lista_compras_server/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.0/lib/active_record/transactions.rb:301:in `rollback_active_record_state!'
    from /opt/code/lista_compras_server/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.0/lib/active_record/transactions.rb:285:in `save'
    from /opt/code/lista_compras_server/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.0/lib/active_record/persistence.rb:34:in `create'
    from (irb):4
    from /opt/code/lista_compras_server/shared/bundle/ruby/2.2.0/gems/railties-4.2.0/lib/rails/commands/console.rb:110:in `start'
    from /opt/code/lista_compras_server/shared/bundle/ruby/2.2.0/gems/railties-4.2.0/lib/rails/commands/console.rb:9:in `start'
    from /opt/code/lista_compras_server/shared/bundle/ruby/2.2.0/gems/railties-4.2.0/lib/rails/commands/commands_tasks.rb:68:in `console'
    from /opt/code/lista_compras_server/shared/bundle/ruby/2.2.0/gems/railties-4.2.0/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
    from /opt/code/lista_compras_server/shared/bundle/ruby/2.2.0/gems/railties-4.2.0/lib/rails/commands.rb:17:in `<top (required)>'
    from bin/rails:8:in `require'
    from bin/rails:8:in `<main>'2.2.0 :005

About this issue

  • Original URL
  • State: closed
  • Created 9 years ago
  • Comments: 16

Commits related to this issue

Most upvoted comments

You don’t need this gem while using PostgreSQL. Remove it from Gemfile, remove include on your model, and change a bit the migration definition like explained here: http://blog.arkency.com/2014/10/how-to-start-using-uuid-in-activerecord-with-postgresql/

If you want to set a UUID attribute value, you will still trigger the error. Try putting this in an initializer:

module PostgresqlUuidQuotingPatch
  extend ActiveSupport::Concern

  included do
    def _type_cast_with_quoting(value)
      if value.is_a?(UUIDTools::UUID)
        value.to_s
      else
        _type_cast_without_quoting(value)
      end
    end

    alias_method_chain :_type_cast, :quoting
  end
end

ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.send :include, PostgresqlUuidQuotingPatch

(Only tested on ActiveRecord 4.2)