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.
Dan S. (Guest)
on 2009-01-13 18: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.
Ar C. (Guest)
on 2009-01-13 19: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.
Mark Reginald J. (Guest)
on 2009-01-13 19:25
(Received via mailing list)
Dan S. 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.