On 16 February 2011 11:21, Ernie Miller <[email protected]> wrote: > On Feb 16, 2011, at 4:53 AM, Colin Law <[email protected]> wrote: > >> HI >> >> I notice on Rails 3.0.4 that the following two statements generate >> different results. >> >> State.where(:abbreviation => 'TX').where(:abbreviation => 'NE') >> SQL: SELECT `states`.* FROM `states` WHERE (`states`.`abbreviation` = >> 'TX') OR (`states`.`abbreviation` = 'NE') >> >> State.where(:abbreviation => 'TX').where("abbreviation = 'NE'") >> SQL: SELECT `states`.* FROM `states` WHERE (`states`.`abbreviation` = >> 'TX') AND (`states`.`abbreviation` = 'NE') >> >> Using Rails 3.0.3 they both generated AND clauses which seems to me to >> be correct. >> >> Is this a bug or by design please? >> >> Colin > > The OR functionality is new, but intentional. See > Relation::QueryMethods#collapse_wheres. Since the idea of having a > column being equal to two different values doesn't make sense, > equalities with the same left-hand side get combined with OR, which is > more likely the desired result if two scopes are merged and both > include equality conditions on the same column. > > Since your example above used a string in the second where call, it > didn't result in an Arel::Nodes::Equality, so it wasn't ORed.
OK, thanks for that. If that is the way it is supposed to be then that is the way it is supposed to be. It seems odd to me that chaining a second where *adds* to the dataset of the first where. It would seem particularly unexpected in the case items = Model.where( :x => y) ... ... myitems = items.where(:x => z) resulting in myitems having more records than items, and in particular records that do not satisfy :x => z. Note that at this point the code would not necessarily 'know' the details of the first scope. Colin -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en.
