# ARel silently ignoring "where" and "offset" clauses on updates

**URL:** <https://discuss.rubyonrails.org/t/arel-silently-ignoring-where-and-offset-clauses-on-updates/57712>\
**Category:** rubyonrails-core\
**Tags:** patch\
**Created:** [March 6, 2011, 2:11am UTC](https://discuss.rubyonrails.org/t/arel-silently-ignoring-where-and-offset-clauses-on-updates/57712 "2011-03-06T02:11:26Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hugo\_Peixoto](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/hugo_peixoto/32/2146_2.png) [@Hugo\_Peixoto](https://discuss.rubyonrails.org/u/Hugo_Peixoto)\
**Post date:** [March 6, 2011, 2:11am UTC](https://discuss.rubyonrails.org/t/arel-silently-ignoring-where-and-offset-clauses-on-updates/57712/1 "2011-03-06T02:11:26Z")

</div>

Hey,

I've submitted some patches to ARel that (supposedly) fix the following bugs:

[https://rails.lighthouseapp.com/projects/8994/tickets/6058-update\_all-ignores-conditions-when-orders-and-limit-options-are-supplied#ticket-6058-7](https://rails.lighthouseapp.com/projects/8994/tickets/6058-update_all-ignores-conditions-when-orders-and-limit-options-are-supplied#ticket-6058-7)[https://rails.lighthouseapp.com/projects/8994-ruby-on-rails/tickets/6459-arel-update-method-silently-drops-offset-clause#ticket-6459-2](https://rails.lighthouseapp.com/projects/8994-ruby-on-rails/tickets/6459-arel-update-method-silently-drops-offset-clause#ticket-6459-2)

Before applying these patches, ARel has the following behaviour:

ruby \> News.where("highlighted = ?", true).order("highlighted\_at").limit(1).offset(1).update\_all(:highlighted =\> false) UPDATE "news" SET "highlighted" = 'f' WHERE "news"."id" IN (SELECT "news"."id" FROM "news" ORDER BY highlighted\_at LIMIT 1)

Note how both the where("highlighted = ?", true) and offset(1) clauses are ignored. After the patches, it yields the following: UPDATE "news" SET "highlighted" = 'f' WHERE (highlighted = 't') AND "news"."id" IN (SELECT "news"."id" FROM "news" ORDER BY highlighted\_at LIMIT 1 OFFSET 1)

Feedback on these would be appreciated.

Hugo Peixoto

---

<div class="post-metadata">

**Author:** ![Adam\_Wrobel1](https://avatars.discourse-cdn.com/v4/letter/a/a183cd/32.png) [@Adam\_Wrobel1](https://discuss.rubyonrails.org/u/Adam_Wrobel1)\
**Post date:** [March 6, 2011, 10:10am UTC](https://discuss.rubyonrails.org/t/arel-silently-ignoring-where-and-offset-clauses-on-updates/57712/2 "2011-03-06T10:10:31Z")

</div>

I think the "WHERE (highlighted = 't')" part should be put in the inner query for this to work correctly.

The query you've shown us selects id of the first highlighted\_at object. It might or might not pass the update condition and so the complete query will update one or no rows, even though further in the defined order there might be other rows that would pass the update condition.

Adam

---

<div class="post-metadata">

**Author:** ![Hugo\_Peixoto](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/hugo_peixoto/32/2146_2.png) [@Hugo\_Peixoto](https://discuss.rubyonrails.org/u/Hugo_Peixoto)\
**Post date:** [March 6, 2011, 10:59am UTC](https://discuss.rubyonrails.org/t/arel-silently-ignoring-where-and-offset-clauses-on-updates/57712/3 "2011-03-06T10:59:24Z")

</div>

I believe you are right. I'm replacing the patches.

Hugo Peixoto
