
Product
Rust Support Now in Beta
Socket's Rust support is moving to Beta: all users can scan Cargo projects and generate SBOMs, including Cargo.toml-only crates, with Rust-aware supply chain checks.
active_record_uuid
is a nice gem that add uuid supports to your activerecord
models (MySQL). It allows you to store uuid in various formats: binary (16 bytes), base64 (24 bytes), hexdigest (32 bytes), or string (36 bytes), and query back with uuid string.
The performance issues arises when you store primary key in large data size. You have two options:
auto-increment
primary key along with uuid columnuuid
primary key, but store in binary formatYou have various choices when storing uuid:
Check this out for more detail, http://www.mysqlperformanceblog.com/2007/03/13/to-uuid-or-not-to-uuid/.
Add this line to your application's Gemfile:
gem 'active_record_uuid'
And then execute:
$ bundle
Or install it yourself as:
$ gem install active_record_uuid
ActiveRecordBase::UuidBase
and UuidBaseHelper
are depreciated. Right now, you can configure your model by using has_uuid
and inherit from ActiveRecord::Base
. Check out the usage below.
uuid_config
is fine, but I like to use has_uuid
and passing in options instead.
If you need to use uuid
column as primary key, don't add any :primary_key
in migration file, because it will make that column as auto-increment integer primary key. Instead, do something like this:
create_table :posts, :force => true, :id => false do |t|
t.string :uuid, :limit => 36
t.string :text
t.timestamps
end
$ rake db:add_primary_keys # Add primary keys to all models
$ rake db:add_primary_key[model_name] # Add primary key to a model
In order for the gem to work well, you need to specify the column type
and limit
correctly according to your has_uuid
(store_as
option).
create_table :posts, :force => true, :id => false do |t|
t.binary :uuid, :limit => 16 # `must set to the correct value`
t.string :text
t.timestamps
end
class Post < ActiveRecord::Base
has_uuid :primary_key => true, :store_as => :binary
end
You can configure using ActiveRecordUuid.configure
, and it will apply to any models which use has_uuid
. Each model can overwrite the general options by passing options into has_uuid
. The following are default values:
ActiveRecordUuid.configure do
column :uuid # :uuid is default
primary_key true # false is default
association false # false is default
generator :timestamp # :timestamp is default
store_as :string # :string is default
hook :before_create # :before_validation is default
end
There's a config generator that generates the default configuration file into config/initializers directory. Run the following generator command, then edit the generated file.
$ rails g active_record_uuid:config
To use uuid in your model, call has_uuid
in your model.
class Post < ActiveRecord::Base
has_uuid :primary_key => true, :hook => :before_create
end
# create a post with auto-generated uuid
post = Post.new(:text => "Manual uuid")
post.save
post.uuid # "79f8a42e-ae60-11e1-9aa9-0026b90faf3c"
# create a post with manual uuid
post = Post.new(:text => "Manual uuid")
post.uuid = "79f8a42e-ae60-11e1-9aa9-0026b90faf3c"
post.save
# assign a uuid
post.assign_uuid
# assign a uuid and save immediately
post.assign_uuid!
# check the uuid value is valid or not
post.uuid_valid?
# generate a new uuid
Post.generate_uuid
class PostBinary < ActiveRecord::Base
has_uuid :primary_key => true, :store_as => :binary
end
post = PostBinary.create(:text => "Binary uuid1")
# INSERT INTO `post_binaries` (`created_at`, `text`, `updated_at`, `uuid`) VALUES ('2012-06-20 17:32:47', 'Binary uuid1', '2012-06-20 17:32:47', x'4748f690bac311e18e440026b90faf3c')
post.uuid # "4748f690-bac3-11e1-8e44-0026b90faf3c"
# it works as usual for finding records
PostBinary.find_by_uuid(post.uuid)
PostBinary.where(:uuid => post.uuid)
PostBinary.find(post)
PostBinary.find(post.uuid)
PostBinary.find([post.uuid])
post.comments.create(:text => "Comment 1")
# access the value that stored in db
post.reload
post.attributes_before_type_cast["uuid"]["value"]
has_uuid
column
optionSet the column name to store uuid value.
primary_key
optionSpecify whether the value of column
is primary key or not.
generator
optionSpecify which generator amongs [:random, :timestamp]
used to generate uuid
value.
store_as
optionSpecify which format to store. Possible values are [:binary, :base64, :hexdigest, :string]
.
hook
optionSpecify the activerecord hook [:after_initialize, :before_validation, :before_create]
to generate uuid value. Assign it to :after_intialize
when you want to associate this record to its children.
assoication
optionWhen you set this option to true
, it expects you have foreign_keys with _uuid
. Therefore, you don't have to pass :foreign_key
option inside association methods
class Author < ActiveRecord::Base
has_uuid :primary_key => true, :association => true
has_and_belongs_to_many :posts
end
class Post < ActiveRecord::Base
has_uuid :primary_key => true, :association => true
has_many :comments
has_one :comment
has_and_belongs_to_many :authors
end
class Comment < ActiveRecord::Base
has_uuid :primary_key => true, :association => true
belongs_to :post
end
# overwrite :foreign_key option and add additional option
class Post < ActiveRecord::Base
has_uuid :primary_key => true, :association => true
has_many :comments, :foreign_key => "comment_id", :inverse_of => :post
end
This gem will set the format for dumping the database schema to :sql
. This means that it is no longer dump the schema to ruby code but database-independent version, db/structure.sql
. Therefore, you would not have any problems when running the tests.
FAQs
Unknown package
We found that active_record_uuid demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Socket's Rust support is moving to Beta: all users can scan Cargo projects and generate SBOMs, including Cargo.toml-only crates, with Rust-aware supply chain checks.
Product
Socket Fix 2.0 brings targeted CVE remediation, smarter upgrade planning, and broader ecosystem support to help developers get to zero alerts.
Security News
Socket CEO Feross Aboukhadijeh joins Risky Business Weekly to unpack recent npm phishing attacks, their limited impact, and the risks if attackers get smarter.