很想知道我正在做这个简单的错误......
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 [#<Plan id: 54....]
current_user.plans.where("status == ?",nil)
# Plan Load (0.2ms) SELECT "plans".* FROM "plans" WHERE "plans"."user_id" = ? AND (status == NULL) [["user_id", 60]]
# => #<ActiveRecord::AssociationRelation []>
不理解为什么第二个陈述没有找到计划..
答案 0 :(得分:0)
你错过了这样的概念:在条件sql中,替换条件的参数被替换为?
而不是与之比较。因此,请将==
替换为=
。
current_user.plans.where("status = ?",nil)
或简单地说,
current_user.plans.where(status: nil)