brewster / elastictastic

Object-document mapper and lightweight API adapter for ElasticSearch
MIT License
88 stars 13 forks source link

Uniqueness validator? #18

Closed kostia closed 12 years ago

kostia commented 12 years ago

Do one need a uniqueness validator? The simplest one would look like this:

class UniquenessValidator < ActiveModel::EachValidator
  def validate_each(record, attribute, value)
    if (other = record.class.filter(term: {attribute => value}).first) 
        && other.id != record.id
      record.errors.add attribute, :taken
    end
  end 
end

Otherwise it would be great to have an appropriate entry in the wiki (currently disabled somehow).

outoftime commented 12 years ago

I would prefer not to include a uniqueness validator because the client can't guarantee uniqueness in the case of concurrent writes -- I look at it as a pleasant fiction. I'll turn on the wiki, though, thanks!

On Oct 19, 2012, at 12:31, Kostiantyn Kahanskyi notifications@github.com wrote:

Do we need a uniqueness validator? The simplest one would look like this:

class UniquenessValidator < ActiveModel::EachValidator def validate_each(record, attribute, value) if record.class.filter(term: {attribute => value}).any? record.errors.add attribute, :taken end end end

Otherwise it would be great to add have an appropriate entry in the wiki (currently disabled somehow).

— Reply to this email directly or view it on GitHubhttps://github.com/brewster/elastictastic/issues/18.