第十一章:
代码 11.6 为 Relationship 模型添加 belongs_to 关系
spec/models/relationship_spec.rb
->
app/models/relationship_spec.rb
代码 11.16 通过反转的关系实现 user.followers
app/models/user.rb
class User < ActiveRecord::Base . . . has_many :reverse_relationships, foreign_key: "followed_id", classname: "Relationship", dependent: :destroy has_many :followers, through: :reverse_relationships, source: :follower . . . end -> app/models/user.rb
class User < ActiveRecord::Base . . . has_many :reverse_relationships, foreign_key: "followed_id", class_name: "Relationship", dependent: :destroy has_many :followers, through: :reverse_relationships, source: :follower . . . end