在父母的孩子上搜索时,ActiveRecord不会返回

很想知道我正在做这个简单的错误…

current_user = User.find(60) Plan.joins(:user).where("users.id" => current_user.id).where("plans.status" => nil) # Plan Load (8.6ms) SELECT "plans".* FROM "plans" INNER JOIN "users" ON "users"."id" = "plans"."user_id" WHERE "users"."id" = 60 AND "plans"."status" IS NULL # => #<ActiveRecord::Relation [# # 

不明白为什么第二个陈述没有找到计划..

你错过了在条件sql中,替换条件的参数被替换的概念? 没有比较。 所以,将double ==替换为=

 current_user.plans.where("status = ?",nil) 

或简单地说,

 current_user.plans.where(status: nil)