Search code examples
ruby-on-rails-3activerecordarelactive-relation

User.where(:id => [1,2]).where(:id => 2).arel.to_sql unexpected result


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)


Solution

  • 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.