NICE! Arel collapsed the getting and setting to a single query: UPDATE "homes" SET "current" = 'f' WHERE (current = 't' & id != 3).
On Aug 29, 2012, at 11:21 AM, Michael Pavling wrote:
> On 29 August 2012 16:13, Walter Lee Davis <waltd@wdstudio.com> wrote:
>> I have a model that includes a :current boolean. The idea is that there are many home pages that the admin is working on at any given time, but only one is current at a time. At the moment, I have enforced this with the following:
>>
>> before_save :toggle_current
>>
>> def toggle_current
>> if current?
>> Home.where(["current = ? & id != ?", true, self.id]).each do |home|
>> home.update_attribute(:current, false)
>> end
>> end
>> end
>
> you could use "update_all" rather than iterate a collection, which
> would only fire one SQL query.
NICE! Arel collapsed the getting and setting to a single query: UPDATE "homes" SET "current" = 'f' WHERE (current = 't' & id != 3).
Thanks for the suggestion!
Walter
--
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 rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
No comments:
Post a Comment