在父母的儿童进行搜索时的ActiveRecord没有返回 [英] ActiveRecord not returning when searching on parent's children

查看:74
本文介绍了在父母的儿童进行搜索时的ActiveRecord没有返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很想知道它是什么简单的错误,我正在做这一次......

Would love to know what simple error it is that I'm making this time...

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 []>

不理解为什么第二个说法是没有找到该计划。

Not understanding why the second statement isn't finding the plan..

推荐答案

您错过了这个概念,在有条件的SQL,按照条件的参数被替换到位?不比较。因此,更换双 == =

You missed the concept that in conditional sql, the arguments that follow the condition are replaced in place of ? not compared to. So, replace the double == with =.

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

或简称

current_user.plans.where(status: nil)

这篇关于在父母的儿童进行搜索时的ActiveRecord没有返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆