Closed dtelaroli closed 9 years ago
Any suggestions?
I created the this patch and work (returns the same value as string).
module ActiveRecord
module ConnectionAdapters
module PostgreSQL
module OID # :nodoc:
class Uuid < Type::Value # :nodoc:
def type_cast_from_user(value)
#UUIDTools::UUID.serialize(value) if value
value
end
alias_method :type_cast_from_database, :type_cast_from_user
end
end
end
end
end
Any updates on this? I'm having same issue.
I have same problem.
[2] pry(main)> o.save
(0.1ms) BEGIN
(0.2ms) ROLLBACK
TypeError: can't cast UUIDTools::UUID to uuid
from /Users/gary/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/activerecord-4.2.1/lib/active_record/connection_adapters/abstract/quoting.rb:34:in `rescue in type_cast'
+1
+1
+1 to give the same UUID support in Mysql, Postgres and sqlite
Since Rails 4 with PostgreSQL, we can use uuid as our primary key.
Here is my solution, but I only use it on Rails 4.2.1 with PostgreSQL 9.3.
First, create a migration. Be sure this migration before any migrations you want to implement with uuid.
rails g migration enable_uuid_extension
And inside the migration file:
class EnableUuidOsspExtension < ActiveRecord::Migration
def change
enable_extension 'uuid-ossp'
end
end
Then Here is an example Model with uuid:
class CreateOrders < ActiveRecord::Migration
def change
create_table :orders, id: :uuid do |t|
t.uuid :user_id, index: true, foreign_key: true
t.string :full_name
t.string :email
t.string :country
t.string :county
t.string :zip_code
t.string :district
t.string :street_address
t.string :phone
t.decimal :tax, precision: 12, scale: 3
t.decimal :shipping, precision: 12, scale: 3
t.decimal :subtotal, precision: 12, scale: 3
t.decimal :total, precision: 12, scale: 3
t.text :notification_params
t.string :workflow_state
t.string :status
t.string :transaction_id
t.datetime :purchased_at
t.timestamps null: false
end
end
end
It's works fine. Thank you.
Neither the extension nor monkey-patching fixes this for me. I'm using Ruby on Rails 4.2.4, ActiveUuid 0.6.1 and PostgreSQL 9.1. What would be the correct way to fix this?
Ah, for me it is a different function. I'll create a pull-request!
having the same issue, running the migration with create extension requires super user and if I run this on the database from psql it did not help. I also have the latest with above mentioned patch and still i get the error. not sure what is wrong
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)
wangthony, this worked for me as far as getting passed this error. However, I now get: " type modifier is not allowed for type "uuid"", would this be related to this Issue?
not sure… what does the stack trace look like?
On Nov 14, 2016, at 10:17 AM, abdalaklinch notifications@github.com wrote:
wangthony, this worked for me as far as getting passed this error. However, I now get: " type modifier is not allowed for type "uuid"", would this be related to this Issue?
— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/jashmenn/activeuuid/issues/60#issuecomment-260415634, or mute the thread https://github.com/notifications/unsubscribe-auth/ABP-kw0Ccvf-N0kYsOYAiA41CkRseuNxks5q-KWkgaJpZM4DsrBe.
Rails 4.2 Ruby 2.2.0
The error happens when I execute Model.create. With SQLite works fine.