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 祝福语驱散霉运的话简短