I have a Logo model that has fields of name:string, default:boolean. I want the true value to be unique, so that only one item in the database can be set to true at once. How do
class Model < ApplicationRecord
before_save :ensure_single_default, if: :is_default?
private
def ensure_single_default
self.class.update_all(is_default: false)
end
end
You don't need to check the id because this callback happens before the truthy one is saved.