class User < ActiveRecord::Base
def self.test_by_conditions(id)
User.where(id:id).to_sql
#......
end
def self.test_by_id(id)
User.find(id).to_sql
#......
end
end
User.where(id:1).to_sql
#SELECT "users".* FROM "users" WHERE "users"."id" = 1
User.where(id:1).test_by_conditions(2)
#SELECT "users".* FROM "users" WHERE "users"."id" = 1 AND "users"."id" = 2
User.where(id:1).test_by_id(2)
#User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? AND "users"."id" = ? LIMIT 1 [["id", 1], ["id", 2]]
#ActiveRecord::RecordNotFound: Couldn't find User with 'id'=2 [WHERE "users"."id" = ?]
应该是 Arel 继承了前面的条件,似乎是到同一个 arel.scrope 里面去了,应该算是个 Bug 吧。