Pedro Nascimento
Pedro Nascimento

Reputation: 13906

User.where(:id =>1).where(:id => 2) with ActiveRecord doesn't work

Simply put:

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

How can I make it run:

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

Upvotes: 1

Views: 942

Answers (1)

Bassam Mehanni
Bassam Mehanni

Reputation: 14944

With the help of arel you can do this:

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

Upvotes: 1

Related Questions