I have a Product model, and I want to be able to define a single Product as a "featured" product. How would I go about setting this up? I have a boolean column, "featured", but I haven't a clue how to ensure that only one of them is true at any time.
A:
The piece you are probably missing is ActiveRecord::Base#update_all. I would probably hook into before_save, and do something like this
Product.update_all 'featured = 0', 'featured = 1' if self.featured_changed? && self.featured_change
Matt Briggs
2010-08-05 03:11:01