Rails:使用has_and_belongs_to_many的自联接方案?

我想创建一个拥有很多friendsUsers结构,也是类User

 class User < ActiveRecord::Base has_and_belongs_to_many :friends, class_name: "User" end 

我不需要他们关系的任何细节因此我不使用:throughFriendship 。 但是现在我找不到任何方法来创建相应的数据库(既不使用迁移文件也不使用rails g model User username:string ...命令)。 有任何想法吗?

以下是一些可能有用的资源:

  • RailsCasts第163集关于自我指涉多对多关系的自我指涉协会
  • RailsCasts第47集两个多对多 。 这可能与您尝试完成的内容更为相关
  • 使用HABTM为自我指涉关系创建的要点

我将总结这些链接中的信息:

鉴于您正在描述自引用的多对多关系,您当然最终会得到一个连接表。 通常情况下,连接表应该以这样的方式故意命名,即Rails会自动确定表加入的模型,但是“自引用”部分使这有点尴尬,但并不困难。 您只需指定连接表的名称以及连接列。

您需要使用可能如下所示的迁移创建此表:

 class CreateFriendships < ActiveRecord::Migration def self.up create_table :friendships, id: false do |t| t.integer :user_id t.integer :friend_user_id end add_index(:friendships, [:user_id, :friend_user_id], :unique => true) add_index(:friendships, [:friend_user_id, :user_id], :unique => true) end def self.down remove_index(:friendships, [:friend_user_id, :user_id]) remove_index(:friendships, [:user_id, :friend_user_id]) drop_table :friendships end end 

我不确定是否有创建此表的快捷方式,但最低限度你可以简单地执行rails g migration create_friendships ,并填写self.upself.down方法。

最后在您的用户模型中,您只需添加连接表的名称,如下所示:

 class User < ActiveRecord::Base has_and_belongs_to_many :friends, class_name: "User", join_table: :friendships, foreign_key: :user_id, association_foreign_key: :friend_user_id end 

如您所见,虽然数据库中有连接表,但没有相关的连接模型。

请告诉我这是否适合您。