Ruby on Rails 自我参照协会
示例
自引用关联用于将模型与其自身关联。最常见的例子是管理朋友和他的跟随者之间的关联。
例如
rails g model friendship user_id:references friend_id:integer
现在您可以将模型关联起来;
class User < ActiveRecord::Base has_many :friendships has_many :friends, :through => :friendships has_many :inverse_friendships, :class_name => "Friendship", :foreign_key => "friend_id" has_many :inverse_friends, :through => :inverse_friendships, :source => :user end
另一个模型看起来像;
class Friendship < ActiveRecord::Base belongs_to :user belongs_to :friend, :class_name => "User" end
热门推荐
10 小红书平安祝福语简短
11 生日祝福语大全女孩简短
12 收生日红包祝福语 简短
13 领证幽默祝福语简短
14 法考面试祝福语简短
15 老哥出门祝福语简短语
16 送灯祝福语简短独特
17 幼儿狗年祝福语大全简短
18 好听的元旦简短祝福语