Ruby on Rails Wednesday, August 29, 2012

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.

--
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