On Wed, Feb 16, 2011 at 8:59 AM, Aaron Patterson <aa...@tenderlovemaking.com
> wrote:

> On Wed, Feb 16, 2011 at 06:21:54AM -0500, Ernie Miller wrote:
> > On Feb 16, 2011, at 4:53 AM, Colin Law <clan...@googlemail.com> 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.
>
> The intentional change fixes this bug:
>
>
> https://rails.lighthouseapp.com/projects/8994-ruby-on-rails/tickets/4598-default_scope-treats-hashes-and-relations-inconsistently-when-overwriting
>
> If there is a different way to fix that ticket, I'm happy to apply
> patches.
>

Having where behave differently depending on whether the column was already
used elsewhere in the relation is pure madness.  If you do:

  relation.where(:column => 1).all

All records returned by it should have 1 as the value of column, regardless
of the contents of relation.  Using where should filter the relation, it
should always return a subset of the receiver.

FWIW, in Sequel, #where always operates as a filter (uses AND), and you can
call #or if you want to use OR.

Jeremy

-- 
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 rubyonrails-core@googlegroups.com.
To unsubscribe from this group, send email to 
rubyonrails-core+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-core?hl=en.

Reply via email to