我一直在研究Hartl的书,我不知道这是否源于我在Rails或数据库原理方面的新手,但我还没有完全理解为了指定外键而反转表的目的。
在本书中,我们引用了一个类似this的表格。我们确定我们需要将follower_id作为用户模型中的外键,因此我们将其指定为:
has_many:relationships,foreign_key:“follower_id”,dependent :: destroy
进一步下来我们确定我们还需要将followed_id作为外键,但不是简单地将followed_id标识为额外的外键,我们欺骗一个翻转的表:
has_many:reverse_relationships,foreign_key:“followed_id”,class_name:“Relationship”,依赖:: destroy
我很难确定这是一个rails约定还是db关系所需。