mardi 31 mai 2016

Update record if checkbox is checked

I'm trying to check if the checkbox has been selected, and if so to then update all records where a particular column (update_checkbox - a boolean value in my table) is true.

The main aim is so only one record in the column can ever be 'true', and when a new record is created/edited and the checkbox checked then this record will be true and turn the other to false.

Under models/article.rb:

class Article < ActiveRecord::Base
  if :update_checkbox == '1'
    #find records where update_checkbox is true & update to false
    Article.where(:update_checkbox => true).update_all(:update_checkbox => false)
  end
end

I can update my records fine but it's the 'if checkbox is checked' part I'm having trouble with - currently when I create a new record with update_checkbox checked the row with this flag set to true is not being set to false. Any help is appreciated!




Aucun commentaire:

Enregistrer un commentaire