Forum: Ruby on Rails Quick validation question

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
30d65340ad6d7fa42aeb8ef7821d05b3?d=identicon&s=25 Dan Smith (jitsudan)
on 2009-01-13 17:13
Hi, I have a model which contains a boolean value.  I would like to set
a validation which only allows this value to be set to true for one
record at a time in the table.  Is it possible to do this with
validates_uniqueness?  or is there another way?

Any help would be appreciated, thanks.
D188e591eac11021329b8821a5f954c7?d=identicon&s=25 Ar Chron (railsdog)
on 2009-01-13 18:13
Is it possible to do this with
> validates_uniqueness?

validates_uniqueness on a boolean would be great if you only ever want 2
records in your table...

Perhaps doing a quick count of records with the value set before saving
your current create/update would achieve what you want.
C64e63b70be7dfed8b0742540b8b27e5?d=identicon&s=25 Mark Reginald James (Guest)
on 2009-01-13 18:25
(Received via mailing list)
Dan Smith wrote:
> Hi, I have a model which contains a boolean value.  I would like to set
> a validation which only allows this value to be set to true for one
> record at a time in the table.  Is it possible to do this with
> validates_uniqueness?  or is there another way?

validates_uniqueness_of :is_root, :if => :is_root should work.

--
Rails Wheels - Find Plugins, List & Sell Plugins -
http://railswheels.com
This topic is locked and can not be replied to.