ActiveRecord HasMany / Belongs可以使用非PrimaryKey列进行关系吗?

时间:2009-11-16 21:40:47

标签: castle-activerecord foreign-key-relationship

我想拥有一个父子层次结构而且我想在父母身上有一个“系列ID”的概念。

所以父母(如果你更新它)将使旧的唯一密钥到期..保留系列ID并插入第二行。

我希望孩子们能够“挂掉”那个系列ID而不是唯一的id。

您知道BelongsTo是否可以链接到非主键(但通过GUID唯一)列?

父:   UniqueId(guid),SeriesId(guid),名称,描述 儿童:   UniqueId(guid),ParentId(始终连接ot UniqueId-Above),名称,描述。

我知道外键关系真的应该在关系的一边有一个“关键”但是,通过我的GUID,它们是关键的......但不是真正的关键。

这不是什么大不了的事,但如果我通过更新它的元数据来“终止”父母,我希望避免将所有孩子“复制”到父母的新副本中。

我是否想做一些不可能发生的事情?

1 个答案:

答案 0 :(得分:-1)

您可以使用belongs_to选项指定关系:

:foreign_key
    Specify the foreign key used for the association. By default this is guessed to be the name of the association with an "_id" suffix. So a class that defines a belongs_to :person association will use "person_id" as the default :foreign_key. Similarly, belongs_to :favorite_person, :class_name => "Person" will use a foreign key of "favorite_person_id".