User.where(:id => [1,2]).where(:id => 2).arel.to_sql 意外结果
在 Rails 控制台中,运行以下命令会产生意外结果:
User.where(:id => [1,2]).where(:id => 2).arel.to_sql
=> "SELECT `users`.* FROM `users` WHERE (`users`.`id` IN (1, 2) OR `users`.`id` = 2)"
我不希望它是一个 OR
。
这个示例是我拥有的无表User
模型的一个非常简化的示例,它定义了一个方法:
def cars
Car.where(:id => list_of_users_car_ids_not_from_the_database)
end
其中Cars
保存在数据库中。然后在控制器中
current_user.cars.find(params[:id])
(好吧,这也是一个简化的示例,但更能代表我想要完成的任务)
From rails console, running the following gives an unexpected result:
User.where(:id => [1,2]).where(:id => 2).arel.to_sql
=> "SELECT `users`.* FROM `users` WHERE (`users`.`id` IN (1, 2) OR `users`.`id` = 2)"
I'm not expecting that to be an OR
.
This example is a very simplified example of a tableless User
model I have that defines a method:
def cars
Car.where(:id => list_of_users_car_ids_not_from_the_database)
end
where Cars
are kept in the database. Then later in a controller
current_user.cars.find(params[:id])
(okay, this is also a simplified example, but much more representative of what I'm trying to accomplish)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这些消息来源称它在 3.0.3 和 3.0.4 之间出现了问题,但他们在 3.0.5 中“修复”了它。
http://www.ruby-forum.com/topic/1089054#new
http://groups.google.com/group/rubyonrails-core/browse_thread /thread/407f746fd1de6636
他们故意“破坏”它(他们正在修复一个错误并认为新行为更好)。不过,它应该在 3.0.5 中恢复。
These sources say it broke between 3.0.3 and 3.0.4, but they've 'fixed' it in 3.0.5.
http://www.ruby-forum.com/topic/1089054#new
http://groups.google.com/group/rubyonrails-core/browse_thread/thread/407f746fd1de6636
They intentionally 'broke' it (they were fixing a bug and thought the new behavior was better). It should be reverted in 3.0.5, though.