自我指涉协会有很多

时间:2012-09-10 14:00:59

标签: ruby-on-rails associations polymorphism

用户可以关注另一个用户或公司。如何在一个表中维护关联,这是一个适用于用户模型的关注者?我也想为公司使用同一张桌子。

添加代码。 我正在使用自引用关联来跟随功能。

class User < ActiveRecord::Base

   has_many :followerships
   has_many :following, :through => :followerships


   has_many :inverse_followerships, :class_name => "Followership", :foreign_key => "following_id"
   has_many :inverse_following, :through => :inverse_followerships, :source => :user
end

1 个答案:

答案 0 :(得分:0)

这可能对跟随用户和公司没有任何帮助,但您是否见过Michael Hartl的Rails Tutorial

基本上,他会引导您构建类似Twitter的应用,包括关注其他用户并被关注。在他的代码非常干净的情况下,看到这种事情的最佳实践可能会有所帮助。

Chapter 11: Following users