No commit activity in last 3 years
No release in over 3 years
This validator tests whether associated collection is going to be empty after saving. It will be passed if at least one association of the specified collection will exist. The validator provides helper method and default error message.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

Runtime

 Project Readme

at_least_one_existence_validator

Build Status Gem Version

Easy to use Rails active model validator which tests whether an associated collection will have any objects after saving. It's useful with one-to-many and many-to-many relationships.

Installation

Add to the Gemfile:

gem 'at_least_one_validator'

After adding, install it

bundle install

Usage

Given you have 'Author' model and 'Book' model respectively. Obviously author can write many books and book can be written by many authors but a book must have at least one author, so eventually you have something like this:

class Author < ActiveRecord::Base
  has_and_belongs_to_many :books
end

class Book   < ActiveRecord::Base
  has_and_belongs_to_many :authors
end

If you want to use at_least_one_existence_validator, you just need to add helper method validates_at_least_one_existence_ofto the model class and list all the collections you want to be validated as parameters. Let's do it:

class Author < ActiveRecord::Base
  has_and_belongs_to_many :books
end

class Book   < ActiveRecord::Base
  has_and_belongs_to_many :authors

  validates_at_least_one_existence_of :authors
end

This code will test whether the authors of the tested book are marked for destruction or authors are already blank. If they are, validator will add default error message.

Configuring

The default error message for English locale is "must have at least one item.". You can specify your own error message adding it with at_least_one: key to localization backend. Using previous example and standard i18n localization mechanism for static content we do the next:

# file project_root/config/locales/en.yml
en:
  activerecord:
    errors:
      models:
        book:
          attributes:
            authors:
              at_least_one: 'must have at least one author.'

Message for at_least_one_existence_validator scoped by book and its authors is changed now.

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request