私はレールチュートリアルからレールを学習しています。Railsチュートリアル3.2 Ch11 SQL構文
def self.from_users_followed_by(user)
followed_user_ids = user.followed_user_ids.join(', ')
where("user_id IN (?) OR user_id = ?", followed_user_ids, user)
end
followed_user_idsにuser_idは戻りません:私は、リスト11.44のコードは、Section 11.3.1で問題を得ました。
stackoverflowで検索したところ、解決方法hereが見つかりました。これは「結合」メソッドを削除するだけで、うまく動作します。
しかし、「結合」メソッドを削除する前にRSPECを実行すると、エラーが発生しない理由が不思議です。リスト11.41で
:
subject { Micropost.from_users_followed_by(user) }
it { should include(own_post) }
it { should include(followed_post) }
RSPECが渡された理由を "参加する" 方法は、SQL文が間違っているでしょうか?
誰も同じ問題がありますか?
アップデート2012年4月3日
私はこの問題をチェックするために、レールコンソールを使用していました。その後、SQLを
User Load (2.6ms) SELECT "users".* FROM "users" INNER JOIN "follows" ON
"follows"."followable_id" = "users"."id" AND "follows"."followable_type" = 'User' WHERE
"follows"."blocked" = 'f' AND "follows"."follower_id" = 1 AND "follows"."follower_type"
= 'User' AND "follows"."followable_type" = 'User'
=> "6, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 21, 22, 23, 24, 25, 26, 27, 28, 29,
30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51,
52, 55, 56, 5"
を得た
は
user = User.first
ids = ids = user.following_users.map(&:id).join(', ')
Collection.where("user_id IN (?)", ids)
結果は空の配列を返す
Collection Load (0.7ms) SELECT "collections".* FROM "collections" WHERE (user_id IN
('6, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 21, 22, 23, 24, 25, 26, 27, 28, 29,
30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51,
52, 55, 56, 5')) ORDER BY collections.created_at DESC
=> []
です。
しかし、私はすべてのrspecを渡しました。まだ考えていない。参加すると仮定すると、
テスト環境はポストグラムでも動作しますか? – Sairam
テスト&開発環境がSQLite上で実行されます。 – Tim
更新しました – Jonathan