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 送给学长毕业祝福语简短