如何使用ruby on rails进行自引用?

时间:2022-10-22 11:24:17

I want to self-referentiate a model in a RoR app but, I don't know exactly how. I want to save a linked list where the next node has the id of the previous one. how can I do this rails way? It is a one-to-one relation.

我想在RoR应用程序中自引用一个模型,但我不知道具体是怎么引用的。我要保存一个链表,其中下一个节点具有前一个节点的id。我怎么才能用这个rails方式呢?它是一对一的关系。

4 个解决方案

#1


53  

The easiest way:

最简单的方法:

class MyModel < ActiveRecord::Base
  belongs_to :parent, :class_name => 'MyModel'
  has_many :children, :class_name => 'MyModel', :foreign_key => 'parent_id'
end

#2


2  

I've spent some time trying to make it work using Rails 3.2.14

我已经花了一些时间尝试使用Rails 3.2.14使它工作

The documentation's suggestion for self-joining associations hasn't worked for belongs_to associations. Adding a foreign key fixed the issue.

文档中关于自连接关联的建议对belongs_to关联不起作用。增加一个外键解决了这个问题。

Class User < ActiveRecord::Base
  has_many :invitees, class_name: 'User', foreign_key: :invited_by
  belongs_to :host, class_name: 'User', foreign_key: :invited_by
end

#3


2  

rails 5

rails 5

add column xxx_id in users table:

在users表中增加列xxx_id:

in migration file:

在迁移文件:

add_reference :users, :xxx, index: true

and add code in User model

并在用户模型中添加代码

has_many :users, class_name: 'User', foreign_key: 'xxx_id'
belongs_to :manager, class_name: 'User', foreign_key: 'xxx_id'

If you don't have a manager for every user, you need to add optional: true.

如果没有针对每个用户的管理器,则需要添加optional: true。

'foreign_key' is not necessary. By default this is guessed to be the name of this class in lower-case and “_id” suffixed.

“foreign_key”并不是必要的。默认情况下,这是这个类的小写名称,后面加上“_id”。

if foreign_key is user_id, user don't have manager necessary. the result is:

如果foreign_key是user_id,则不需要manager。其结果是:

has_many :users, class_name: 'User'
belongs_to :manager, class_name: 'User', optional: true

#4


0  

Also check out this tutorial by Ryan Bates on self referential association here. Hck's answer will work but for me, I need a JOIN table and so I use a has_many through association of Rails. Good luck!

也可以在这里查看Ryan Bates关于自我参照关联的教程。Hck的答案是可行的,但是对于我来说,我需要一个连接表,所以我通过Rails的关联使用了一个has_many。好运!

#1


53  

The easiest way:

最简单的方法:

class MyModel < ActiveRecord::Base
  belongs_to :parent, :class_name => 'MyModel'
  has_many :children, :class_name => 'MyModel', :foreign_key => 'parent_id'
end

#2


2  

I've spent some time trying to make it work using Rails 3.2.14

我已经花了一些时间尝试使用Rails 3.2.14使它工作

The documentation's suggestion for self-joining associations hasn't worked for belongs_to associations. Adding a foreign key fixed the issue.

文档中关于自连接关联的建议对belongs_to关联不起作用。增加一个外键解决了这个问题。

Class User < ActiveRecord::Base
  has_many :invitees, class_name: 'User', foreign_key: :invited_by
  belongs_to :host, class_name: 'User', foreign_key: :invited_by
end

#3


2  

rails 5

rails 5

add column xxx_id in users table:

在users表中增加列xxx_id:

in migration file:

在迁移文件:

add_reference :users, :xxx, index: true

and add code in User model

并在用户模型中添加代码

has_many :users, class_name: 'User', foreign_key: 'xxx_id'
belongs_to :manager, class_name: 'User', foreign_key: 'xxx_id'

If you don't have a manager for every user, you need to add optional: true.

如果没有针对每个用户的管理器,则需要添加optional: true。

'foreign_key' is not necessary. By default this is guessed to be the name of this class in lower-case and “_id” suffixed.

“foreign_key”并不是必要的。默认情况下,这是这个类的小写名称,后面加上“_id”。

if foreign_key is user_id, user don't have manager necessary. the result is:

如果foreign_key是user_id,则不需要manager。其结果是:

has_many :users, class_name: 'User'
belongs_to :manager, class_name: 'User', optional: true

#4


0  

Also check out this tutorial by Ryan Bates on self referential association here. Hck's answer will work but for me, I need a JOIN table and so I use a has_many through association of Rails. Good luck!

也可以在这里查看Ryan Bates关于自我参照关联的教程。Hck的答案是可行的,但是对于我来说,我需要一个连接表,所以我通过Rails的关联使用了一个has_many。好运!