というわけで、ユーザーが作者(他のユーザー)をフォローできるシステムを作っています。
ユーザー モデル:
class User < ActiveRecord::Base
has_many :author_following, class_name: 'Following'
has_many :following, through: :author_following, source: :author
has_many :followers, foreign_key: 'author_id', through: :author_following, source: :user
end
以下のモデル:
class Following < ActiveRecord::Base
belongs_to :user
belongs_to :author, foreign_key: 'author_id', class_name: "User"
end
問題:フォローしている著者のリストを取得できますが、フォロワーのリストを取得できます。
与えられた:u
他のユーザーをフォローしており、フォロワーを持つ有効なユーザーです
u.following
次の SQL を生成します。
SELECT "users".* FROM "users" INNER JOIN "followings" ON "users"."id" = "followings"."author_id" WHERE "followings"."user_id" = $1 [["user_id", 1]]
どちらが正しい..
u.followers
次の SQL を生成します。
SELECT "users".* FROM "users" INNER JOIN "followings" ON "users"."id" = "followings"."user_id" WHERE "followings"."user_id" = $1 [["user_id", 1]]
どちらが間違っています..
理想的には、この SQL はWHERE "followings"."author_id" = $1