0

我有两个课程:TopicBuy::Topic。后者是前者的子类,通过外键 ( Buy::Topic.topic_id == Topic.id)。所以本质上这是使用类表继承模式。

Buy::Topic有自己的和指向父对象id的外键。topic_idTopic

但是,我想确保 id与来自父级的 idBuy::Topic相同,如下所示:topic_id

#<Topic id: 22598>#<Buy::Topic id: 22598, topic_id:22598

这在 Rails 中可行吗?这对于 SEO 目的更好,并且可以更轻松地处理 Cancan 的load_and_authorize_resource(有时 cancan 使用两者的错误 id 来查找东西)。

UDPATE:

这个模式已经设置并完成了很长时间,所以现在不太可能将其更改为 STI。

4

2 回答 2

1

Doing this will actively destroy your CTI and might even create an anti-pattern!

My argument is that if,

Buy::Topic.id == Buy::Topic.topic_id == Topic.id

then you create a one-to-one relationship between Buy::Topic and Topic.

This means that no other class can be composed of Buy::Topic because the id could clash with some Topic.id.

Since Buy::Topic is a subclass of Topic, you really only have a single conceptual entity now. Except you have two tables and some crazy indexes to get you there.

于 2012-03-06T04:32:20.133 回答
0

伙计,这没关系,只需调用您的列 topic_parent_id 并创建如下关联:

belongs_to :parent, :class_name => '主题', :foreign_key => 'topic_parent_id'

于 2012-03-06T09:15:37.523 回答