使用UUID在铁轨和多形体关系的主键 [英] using UUID as primary key in rails and polymorph relationships

查看:176
本文介绍了使用UUID在铁轨和多形体关系的主键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要创建一个Rails 3应用程序,将分散,我需要使用 UUID 作为 主键作为我的表,是什么将是最好的宝石,插件作业。我也想知道是否有可能使的ActiveRecord 不使用它的polymorphicable_type列,因为我使用UUID的情况下多态关系。

I'm creating a rails 3 application that will be decentralized and I need to use UUID as primary key for my tables, what would be the best gem, plugin for the Job. I also would like to know if it is possible to make in ActiveRecord polymorphic relationships without using the polymorphicable_type column for it, given the case that I'm using UUID.

我创建了一个演示 http://github.com/boriscy/uuidrails3 使用UUID作为密钥,您应检查模块UUIDHelper内的lib /和所有的迁移。没有必要添加在数据库中的主键,只是一个索引,因为主键验证字段的唯一性,但是我们并不需要与UUID

I have created a demo http://github.com/boriscy/uuidrails3 that uses UUID as keys, you should check the module UUIDHelper inside lib/ and also all the migrations. There is no need to add a primary key in the database, just an index, because primary keys verify uniqueness of the field, but we don't need that with UUID.

推荐答案

把的 lib目录此文件,并添加这是一个初始化:

Put this file in the lib directory, and add this as an initializer:

class ActiveRecord::Base
  set_primary_key :uuid

  before_create :set_uuid

  def set_uuid
    self.uuid = UUID.generate(:compact)
  end
end

对于关系,弗拉德是正确的ActiveRecord的需要知道哪一个表中的记录是找到它。如果你想这样的功能,尝试其他的东西像MongoMapper。

As for the relationships, vlad is right that ActiveRecord needs to know which table a record is in to find it. If you want this kind of functionality, try something else like MongoMapper.

这篇关于使用UUID在铁轨和多形体关系的主键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆