本文介绍了Rails 3中的自引用模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个实体模型,我想显示实体之间的连接.也就是说,实体1连接到实体2.

I have an Entity model and I want to display connections between the Entities. ie, Entity 1 is connected to Entity 2.

我现在的想法是在两个称为Connection的连接之间创建一个连接模型,并使它像传统的Rails连接表一样工作.除了具有entity_one_id和entity_two_id列之外,然后在实体和连接之间建立多对多关系.

My thinking, right now, is to create a join model between the two called Connection and have it work like a traditional rails join table. Except have the columns be entity_one_id and entity_two_id, then establish a many-to-many relationship between Entity and Connection.

这似乎是一种非常优雅的方法.我想知道是否有人有更好的主意?也许我只是没有看到更多类似rails-esque的东西?

This seems like a really not-elegant way to do this. I was wondering if anyone had any better ideas? Maybe something more rails-esque that I'm just not seeing?

推荐答案

这是最常用的方法.如果一个实体仅连接到另一个模型,则可以使用链表,树状结构.

That's the most-common way to do it. If an entity is only ever connected to one other model, you could use a linked-list, tree-like structure.

查看赖安·贝茨(Ryan Bates)关于自动加入模型的Railscast .它处理的是类似社交网络的系统,但仍然具有您需要的原则,并提供了一个很好的起点

Check out Ryan Bates' Railscast on self-joining models. It deals with a social-network-like system, but it still has principles you'll need and provides a great starting point

这篇关于Rails 3中的自引用模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 09:41