On 16 February 2011 05:42, Mark Kremer <[email protected]> wrote:
> It may seem a bit odd with the where method chained twice like that, but the
> query with the OR (Rails 3.0.4) will probably get you the result you want
> whereas the query with the AND (Rails 3.0.3) will return 0 records at all
> times (because the same attribute can't have two values).
I disagree that OR is the result one would want if one were to code
this. I agree it is apparently a useless query and obviously one
would not code it exactly like this. If the sql for this is
incorrectly generated however, it is likely that a more subtle, and
useful, query may also be encoded wrongly. The principle of chained
where is that the first one is performed, then the next is performed
on the results of the first. At least that is how I understand it.
Consider:
State.where(:abbreviation => 'TX').where("abbreviation = 'NE'")
This (correctly in my view) uses AND in the query.
As 3.0.4 stands at the moment the above yields a different result to
State.where(:abbreviation => 'TX').where(:abbreviation => 'NE')
which seems incorrect to me.
I will post on rails-core to see what the response is there.
Colin
>
> On 15-2-2011 10:07, Colin Law wrote:
>>
>> On 14 February 2011 21:03, Jeremy<[email protected]> wrote:
>>>
>>> I've noticed a rather insidious 3.0.4 change to the way that chained
>>> where methods that reference the same attribute are handled. Does
>>> anyone know where I can find more information about where/when this
>>> change came in, and the justification for it? I haven't been able to
>>> find a changelog mentioning this modification.
>>>
>>> Essentially, when you chain two where methods together that reference
>>> the same attribute, the SQL query generated uses an OR on that
>>> attribute, rather than AND.
>>>
>>> Example:
>>>
>>> Rails 3.0.3
>>> State.where(:abbreviation => 'TX').where(:abbreviation => 'NE')
>>> SQL: SELECT `states`.* FROM `states` WHERE (`states`.`abbreviation` =
>>> 'TX') AND (`states`.`abbreviation` = 'NE')
>>>
>>> Rails 3.0.4
>>> State.where(:abbreviation => 'TX').where(:abbreviation => 'NE')
>>> SQL: SELECT `states`.* FROM `states` WHERE (`states`.`abbreviation` =
>>> 'TX' OR `states`.`abbreviation` = 'NE')
>>
>> I can confirm I see the same issue on 3.0.4 (not tried on 3.0.3). It
>> looks like a bug to me.
>>
>> Colin
>>
>
> --
> You received this message because you are subscribed to the Google Groups
> "Ruby on Rails: Talk" 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-talk?hl=en.
>
>
--
You received this message because you are subscribed to the Google Groups "Ruby
on Rails: Talk" 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-talk?hl=en.