rails チュートリアル14章のフォロー機能に関しての質問です。
URL: https://railstutorial.jp/chapters/following_users?version=5.0#sec-a_problem_with_the_data_model
経緯:
フォロー機能(Twitter的な)を作成するために、Userモデルで自己結合させようとしています。
そのためにRelationshipという中間テーブルを作成しました。カラムはfollowed_idとfollower_idの二つです。follower_idは自分のことをfollowしてくれている人のid、followed_idは自分がfollowしている人のidです。
コード:
User
Ruby
1#User.rb 2has_many :active_relationships, class_name:"Relationship",foreign_key: "followed_id",dependent: :destroy 3has_many :followings, through: :active_relationships, source: :true 4has_many :passive_relationships, class_name: "Relationship",foreign_key: "follower_id" ,dependent: :destroy 5has_many :followers, through: :passive_relationships, source: :true
Ruby
1#Relationship.rb 2belongs_to :follower, class_name: "User" 3belongs_to :followed, class_name: "User" 4validates :follower_id, presence: true 5validates :followed_id, presence: true
質問事項:
Userモデルにおいて、なぜactive_relationshipsのforeign_keyがfollowed_id、passive_relationshipsがfollower_idなのですか。
明確な理由がわかりません。
何卒宜しくお願い申し上げます。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/09/18 05:11
2017/09/18 05:37