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 祝福语结婚诗句大全简短