Class: ActiveID::Type::BinaryUUID

Inherits:
Base
  • Object
show all
Defined in:
lib/active_id/type.rb

Overview

ActiveRecord’s attribute type which serializes UUIDs as binaries. Useful for RDBSes which do not support UUIDs natively (i.e. MySQL, SQLite3).

UUIDs serialized as binaries are more space efficient (16 bytes vs 36 characters of their text representation), which may also lead to performance boost if given column is indexed (a bigger piece of index can be kept in memory). The downside is that this representation is less readable for humans who access serialized values outside Rails (i.e. in a database console).

Accessing in database console

In MySQL (but not in MariaDB), there is a BIN_TO_UUID() function which converts binaries to UUID strings. There is a feature request in MariaDB’s issue tracker to add a similar feature.

Caveat

Does not work with PostgreSQL adapter. Nevertheless, there should not be any good reason to use BinaryUUID with PostgreSQL. Open a feature request if you find any.

In PostgreSQL, StringUUID attribute type is recommended as it is compatible with Postgres-specific UUID data type.

Instance Attribute Summary

Attributes inherited from Base

#storage_type

Instance Method Summary collapse

Methods inherited from Base

#cast_value, #deserialize, #initialize

Constructor Details

This class inherits a constructor from ActiveID::Type::Base

Instance Method Details

#serialize(value) ⇒ Object



69
70
71
# File 'lib/active_id/type.rb', line 69

def serialize(value)
  s_serialize(cast_to_uuid(value)&.raw)
end