{"id":15957144,"url":"https://github.com/mamantoha/validates_nested_uniqueness","last_synced_at":"2026-02-27T02:33:32.978Z","repository":{"id":66224790,"uuid":"412470596","full_name":"mamantoha/validates_nested_uniqueness","owner":"mamantoha","description":"Library for validating nested uniqueness in Rails.","archived":false,"fork":false,"pushed_at":"2026-01-14T11:31:50.000Z","size":58,"stargazers_count":6,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2026-01-14T15:47:15.208Z","etag":null,"topics":["rails","ruby","ruby-on-rails"],"latest_commit_sha":null,"homepage":"","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mamantoha.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2021-10-01T13:09:00.000Z","updated_at":"2026-01-14T11:31:55.000Z","dependencies_parsed_at":"2024-01-30T09:43:26.498Z","dependency_job_id":"60c6d049-8708-4c32-b0aa-572d8fece408","html_url":"https://github.com/mamantoha/validates_nested_uniqueness","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"purl":"pkg:github/mamantoha/validates_nested_uniqueness","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mamantoha%2Fvalidates_nested_uniqueness","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mamantoha%2Fvalidates_nested_uniqueness/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mamantoha%2Fvalidates_nested_uniqueness/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mamantoha%2Fvalidates_nested_uniqueness/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mamantoha","download_url":"https://codeload.github.com/mamantoha/validates_nested_uniqueness/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mamantoha%2Fvalidates_nested_uniqueness/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29883102,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-26T23:51:21.483Z","status":"online","status_checked_at":"2026-02-27T02:00:06.759Z","response_time":57,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["rails","ruby","ruby-on-rails"],"created_at":"2024-10-07T13:40:41.356Z","updated_at":"2026-02-27T02:33:32.962Z","avatar_url":"https://github.com/mamantoha.png","language":"Ruby","readme":"# Validates Nested Uniqueness\n\n[![Ruby](https://github.com/mamantoha/validates_nested_uniqueness/actions/workflows/ruby.yml/badge.svg)](https://github.com/mamantoha/validates_nested_uniqueness/actions/workflows/ruby.yml)\n[![GitHub release](https://img.shields.io/github/release/mamantoha/validates_nested_uniqueness.svg)](https://github.com/mamantoha/validates_nested_uniqueness/releases)\n[![License](https://img.shields.io/github/license/mamantoha/validates_nested_uniqueness.svg)](https://github.com/mamantoha/validates_nested_uniqueness/blob/main/LICENSE)\n\nValidates whether associations are uniqueness when using `accepts_nested_attributes_for`.\n\nSolves the original Rails issue [#20676](https://github.com/rails/rails/issues/20676).\n\nThis issue is very annoying and still open after years. And probably this will never be fixed.\n\nThis code is based on solutions proposed in the thread. Thanks everyone ❤️.\n\n## Installation\n\n`validates_nested_uniqueness` works with Rails 6.1 onwards.\n\nAdd this to your Rails project's `Gemfile`:\n\n```ruby\ngem 'validates_nested_uniqueness', git: 'https://github.com/mamantoha/validates_nested_uniqueness'\n```\n\n## Usage\n\nMaking sure that only one `city` of the `country` can be named \"NY\".\n\n```ruby\nclass City \u003c ActiveRecord::Base\n  belongs_to :country\nend\n\nclass Country \u003c ActiveRecord::Base\n  has_many :cities, dependent: :destroy\n  accepts_nested_attributes_for :cities, allow_destroy: true\n\n  validates :cities, nested_uniqueness: {\n    attribute: :name,\n    scope: [:country_id],\n    case_sensitive: false\n  }\nend\n\ncountry = Country.new(name: 'US', cities: [City.new(name: 'NY'), City.new(name: 'NY')])\ncountry.save\n# =\u003e false\n\ncountry.errors\n# =\u003e #\u003cActiveModel::Errors [#\u003cActiveModel::NestedError attribute=cities.name, type=taken, options={:value=\u003e\"NY\", :message=\u003enil}\u003e]\u003e\n\ncountry.errors.messages\n# =\u003e {\"cities.name\"=\u003e[\"has already been taken\"]}\n```\n\nConfiguration options:\n\n- `:attribute` - Specify the attribute name of associated model to validate.\n- `:scope` - One or more columns by which to limit the scope of the uniqueness constraint.\n- `:case_sensitive` - Looks for an exact match. Ignored by non-text columns (`true` by default).\n- `:message` - A custom error message (default is: \"has already been taken\").\n- `:error_key` - A custom error key to use (default is: `:taken`).\n\n## Sponsorship\n\nThis library is sponsored by [Faria Education Group](https://github.com/eduvo), where it was originally developed and utilized in a production project. It has been extracted and refined for open-source use.\n\n## Contributing\n\n1. Fork it (\u003chttps://github.com/mamantoha/validates_nested_uniqueness/fork\u003e)\n2. Create your feature branch (git checkout -b my-new-feature)\n3. Commit your changes (git commit -am 'Add some feature')\n4. Push to the branch (git push origin my-new-feature)\n5. Create a new Pull Request\n\n## Contributors\n\n- [mamantoha](https://github.com/mamantoha) Anton Maminov - creator, maintainer\n\n## License\n\nCopyright: 2021-2025 Anton Maminov (anton.maminov@gmail.com)\n\nThis library is distributed under the MIT license. Please see the LICENSE file.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmamantoha%2Fvalidates_nested_uniqueness","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmamantoha%2Fvalidates_nested_uniqueness","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmamantoha%2Fvalidates_nested_uniqueness/lists"}