Question

I have ancestry tree in my app.

I want to implement uniqueness for records only on the same level.

let me explain

Records:

 #<Folder id: 1, title: "folder1", ... ancestry: nil>,
 #<Folder id: 2, title: "folder2", ... ancestry: "1">,
 #<Folder id: 3, title: "folder3", ... ancestry: "1/2">

if i want to to create folder (:title => 'folder2', :ancestry => '1') it should raise an error title is olready taken,

but if i create folder (:title => 'folder2', :ancestry => nil or "1/2") it should create new record.

probably someone have this problem, and can give advice or answer

(in ancestry method @elem.siblings returns all records on @elem level)

Was it helpful?

Solution

The uniqueness validator can accept a scope parameter:

validates_uniqueness_of :title, :scope => :ancestry

It will now validate that title us unique amongst records with the same ancestry value.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top