使用ActiveRecord的User.where(:id => 1).where(:id => 2)不起作用

时间:2011-12-22 17:15:39

标签: ruby-on-rails ruby activerecord

简单地说:

pry(main)> User.where(:id =>1).where(:id => 2).first
 User Load (0.4ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 2 LIMIT 1
 #user 2 is returned

如何让它运行:

pry(main)> User.where(:id =>1).where(:id => 2).first
 User Load (0.4ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 1 AND "users"."id" = 2 LIMIT 1
#this way no user is returned, because I'm narrowing it down as much as possible

1 个答案:

答案 0 :(得分:1)

arel的帮助下,你可以这样做:

t = User.arel_table
User.where(t[:id].eq("1").and(t[:id].eq("2")))