百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
A

activeuuid

> 编程语言
开源

Rails 中的二进制 UUID 密钥

335 stars0 点赞2 次浏览
访问官网GitHub

工具介绍

Rails 中的二进制 UUID 密钥

activeuuid

Add binary(16) UUIDs to ActiveRecord.

Example

Create a Migration

activeuuid adds the uuid type to your migrations. Example:

ruby
class CreateEmails  false  do |t|
      t.uuid :id, :primary_key => true
      t.uuid :sender_id  # belongs_to :sender

      t.string :subject
      t.text :body

      t.timestamp :sent_at
      t.timestamps
    end
    add_index :emails, :id
  end

  def self.down
    drop_table :emails
  end
end

include ActiveUUID::UUID in your model

ruby
class Email  [Here is a] UUID:  1e8ef774-581c-102c-bcfe-f1ab81872213
>
> A UUID like the one above is 36 characters long, including dashes.  If you store this VARCHAR(36), you're going to decrease compare performance dramatically.  This is your primary key, you don't want it to be slow.
>
> At its bit level, a UUID is 128 bits, which means it will fit into
> 16 bytes, note this is not very human readable, but it will keep
> storage low, and is only 4 times larger than a 32-bit int, or 2
> times larger than a 64-bit int.

Many of the existing examples of how to use UUIDs as primary keys
in Rails use strings rather than bytes (e.g. [3]).

However, this plugin stores the primary keys as bytes. To the
application the keys are represented by a UUIDTools::UUID object.

## Benefits of UUIDs as primary key

* no id conflict during multi-master write
* no locking due to auto-increment
* with time-based UUIDs you can store a timestamp within your UUID
* you can create natural keys (based on the SHA of model attributes)

## Future work
* more transparent support for natural and composite keys
* support for MySQLs `INSERT ... ON DUPLICATE KEY UPDATE` syntax
* support a primary column name other than `id`
* tests

## Inspiration
James Golick's `friendly` is a great gem for NoSQL on MySQL. It's
a great gateway drug to systems like Cassandra for teams that are
already familiar with the ins-and-outs of MySQL.

## Installation

Add this to your `Gemfile`

    gem "activeuuid"

Or get the code here: https://github.com/jashmenn/activeuuid

## References
* [1] http://bret.appspot.com/entry/how-friendfeed-uses-mysql
* [2] http://kekoav.com/blog/36-computers/58-uuids-as-primary-keys-in-mysql.html
* [3] https://gist.github.com/937739
* [4] http://www.codinghorror.com/blog/2007/03/primary-keys-ids-versus-guids.html
* [5] http://krow.livejournal.com/497839.html
* [6] https://github.com/jamesgolick/friendly
* [7] http://tools.ietf.org/html/rfc4122.html#appendix-C

## Dependencies
Rails ~> 3.1.0

## Authors

- Nate Murray
- pyromaniac
- Andrew Kane
- Devin Foley
- Arkadiy Zabazhanov
- Jean-Denis Koeck
- Florian Staudacher
- Schuyler Erle
- Florian Schwab
- Thomas Guillory
- Daniel Blanco Rojas
- Olivier Amblet

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Ruby

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言