This project is archived and is in readonly mode.
Limit is ignored when joins is specified
-
James
Turns out it has nothing to do with the joins.
Animal.limit(1).count
=> 1297Is that expected behavior?
Found this by trying to do (essentially) Animal.limit(1).update_all(...) -- not surprisingly based on the above, it's updating every record.
-
Jan Xie
Model.limit(1).count generate correct sql (select count(*) from model limit 1), it's mysql's behavior:
mysql> select count(*) from knowledges limit 1; +----------+ | count(*) | +----------+ | 842 | +----------+ 1 row in set (0.00 sec) -
James
Yeah -- that makes perfect sense -- forgot that count and size actually modify the query and return a single row.
So what about my specific case with update_all? It builds the update_all query including the "wheres" and discards the limit:
Animal.limit(1).joins(:lot).where("animals.location_id" => 20).where("lots.species" => "Horse").update_all(:location_id => 20)AREL (2.7ms) UPDATE
animalsINNER JOINlotsONlots.id=animals.lot_idSETlocation_id= 20 WHEREanimals.location_id= 20 ANDlots.species= 'Horse' -
Aaron Patterson
- State changed from new to invalid
- Importance changed from to Low
James,
ordermust also be supplied if you want the limit to work on updates.