Search code examples
ruby-on-railsformsparent-childnested-formsvalidates-uniqueness-of

Validate uniqueness of child objects in Rails nested form


I have a form for Library, which has several nested objects for Librarian. My question is:

How can I validate uniqueness of Librarian's name? (there may be multiple librarians for one library, but there should not be the same librarian appears twice). I found a hint but couldn't make it work:

    validates_uniqueness_of :task_name, :scope => :project_id

Please give me an example, because I found pieces to the puzzle but have not been able to put them together.


Solution

  • If Librarian has library_id:

    validates_uniqueness_of :name, :scope => :library_id