4 ms·
Does AR really work like this? users = User.where(:name => "david").limit(20) users.where("age > 29") # SELECT * FROM users # WHERE name
by viraptor 16y ago
Does AR really work like this?
users = User.where(:name => "david").limit(20)
users.where("age > 29")
# SELECT * FROM users
# WHERE name = "david" AND age > 29
# ORDER BY name
# LIMIT 20
It seems a bit counter-intuitive to me. I'd expect that
User.where(:name => "david").limit(20).where("age > 29")
=/=
User.where(:name => "david").where("age > 29").limit(20)
Yeah - just nitpicking, but it caught my attention.
- steveklabnik 16y agoThe query is lazily generated, so that's why they're equal.
- astrange 16y agoWhy should a lazy lookup behave differently than any other one, except that it happens later?
- steveklabnik 16y agoYou're assuming that execution happens on a limit, and it doesn't.
- emmett 16y agoIt depends on whether your mental model is "applying a sequence of filters to a result set" or "editing attributes of a query". If the latter, you'd expect them to be equal and the opposite would be surprising. Just from the point of view of a Rails user, generally I want to do the latter. This will replace things I use .with_scope() for right now, which a "sequence of filters" model wouldn't work as well for.
- jallmann 16y agoOr even: User.where(:name => "david", :age => "> 29").limit(20) Although this way you'd have to parse the '>' out of the second K-V.
- xentronium 16y agoParsing the value is not the best idea (you have to watch the types and still cannot apply the comparison query to string columns). DataMapper resolves the issue via :age.gt => 29 and Sequel got its stylish filter{age > 29} syntax.
- melvinram 16y agoIt may seem counter intuitive at first but here's the thing to understand: > users = User.where(:name => "david").limit(20) Does not actually do any calls or create any kind of sql by itself. When you follow it up by > users.where("age > 29") all you're telling the users object is that you want to add a condition to your next query. You are not telling it to refine the previous query. To get what you want, you'd need to do: > users = User.where(:name => "david").limit(20).all > users.where("age > 29")
- Raphael_Amiard 16y agoWell, you could do it the django way, where you can't filter a queryset after a slice has been taken (eg. limit has been used) This is ten times more logical if you ask me.
- emmett 16y agoIt's very convenient to be able to pass a partially built query, including a limit, into a function which adds more conditions. Why would you not want to be able to do that?
- Raphael_Amiard 16y agoBecause the meaning of it is totally counter intuitive, for one ?
- viraptor 16y agoSure - I know how it works and that it's useful. Still - it breaks the no-surprise rule for me... You could always have stacking functions with both .where and .combine_where, so that: User.where(:name => "david").limit(20).where("age > 29") gives you (with second where starting new level of query): SELECT * FROM (SELECT * FROM user WHERE name = 'david' LIMIT 20) WHERE age > 29 and this (combine_where folding into the same expression): User.where(:name => "david").limit(20).combine_where("age > 29") gives you: SELECT * FROM user WHERE name = 'david' AND age > 29 LIMIT 20 Just like you don't expect `map()[range].map()` or whatever the equivalent in Ruby is, to combine the map calls and move the range to the end.
- goodside 16y agoYou have to keep in mind that unspecified elements of SQL are not guaranteed to be random. There is no situation in which the two queries you supplied, executed sequentially as you "expect", would be guaranteed to produce different results. For any possible set X of 20-or-fewer Davids who are 29 or older, there exists a set of Y of 20 or fewer Davids of unspecified age such that X is the subset of Y obtained by filtering out the Davids in Y who are younger than 29. If your query allows this result, the DBMS is permitted to return it if it deems it more efficient. You cannot assume anything about your result sets. It's best to just pretend every database functions something like the Monkey's Paw: it will grant your wish to the letter, but always in the most horrifying manner that your wish permits.
- deleted 16y ago[deleted]