0

How to validates_uniqueness_of for elasticsearch persistence. I have in a model with the include statement

include Elasticsearch::Persistence::Model

I want to include a uniqueness validation like validates_uniqueness_of :username

When I tried to initialize with a new function NoMethodError: undefined method validates_uniqueness_of for class

Trace

undefined method `validates_uniqueness_of' for #<Profile:0x007fd83c2b8a10>
    from profile.rb:22:in `block in <class:Profile>'
    from /Users/a6001096/.rvm/gems/ruby-2.2.2/gems/activesupport-4.2.4/lib/active_support/callbacks.rb:446:in `instance_exec'
    from /Users/a6001096/.rvm/gems/ruby-2.2.2/gems/activesupport-4.2.4/lib/active_support/callbacks.rb:446:in `block in make_lambda'
    from /Users/a6001096/.rvm/gems/ruby-2.2.2/gems/activesupport-4.2.4/lib/active_support/callbacks.rb:164:in `call'
    from /Users/a6001096/.rvm/gems/ruby-2.2.2/gems/activesupport-4.2.4/lib/active_support/callbacks.rb:164:in `block in halting'
    from /Users/a6001096/.rvm/gems/ruby-2.2.2/gems/activesupport-4.2.4/lib/active_support/callbacks.rb:504:in `call'
    from /Users/a6001096/.rvm/gems/ruby-2.2.2/gems/activesupport-4.2.4/lib/active_support/callbacks.rb:504:in `block in call'
    from /Users/a6001096/.rvm/gems/ruby-2.2.2/gems/activesupport-4.2.4/lib/active_support/callbacks.rb:504:in `each'
    from /Users/a6001096/.rvm/gems/ruby-2.2.2/gems/activesupport-4.2.4/lib/active_support/callbacks.rb:504:in `call'
    from /Users/a6001096/.rvm/gems/ruby-2.2.2/gems/activesupport-4.2.4/lib/active_support/callbacks.rb:92:in `__run_callbacks__'
    from /Users/a6001096/.rvm/gems/ruby-2.2.2/gems/activesupport-4.2.4/lib/active_support/callbacks.rb:778:in `_run_save_callbacks'
    from /Users/a6001096/.rvm/gems/ruby-2.2.2/gems/activesupport-4.2.4/lib/active_support/callbacks.rb:81:in `run_callbacks'
    from /Users/a6001096/.rvm/gems/ruby-2.2.2/gems/elasticsearch-persistence-0.1.8/lib/elasticsearch/persistence/model/store.rb:51:in `save'
    from (irb):5



class Profile
  include Elasticsearch::Persistence::Model

  attribute :user_id
  attribute :username, String, mapping: { index: 'not_analyzed' }
  attribute :image_url

  validates_uniqueness_of :username

end
Maged Makled
  • 1,918
  • 22
  • 25

2 Answers2

0

validates_uniqueness_of is a class method of ActiveRecord

Your Profile class should be defined using elasticsearch-model like this instead

require 'elasticsearch/model'

class Profile < ActiveRecord::Base
  include Elasticsearch::Model

  attribute :user_id
  attribute :username, String, mapping: { index: 'not_analyzed' }
  attribute :image_url

  validates_uniqueness_of :username
end
Val
  • 207,596
  • 13
  • 358
  • 360
  • I'm not using any regular db and using elasticsearch as persistence. Also according to doc here https://github.com/elastic/elasticsearch-rails/tree/master/elasticsearch-persistence you can use validation `validates :title, presence: true` – Maged Makled Mar 08 '16 at 05:12
  • what I'm asking here is what other validation I can use here, I don't seem to find anything else – Maged Makled Mar 08 '16 at 05:20
0

without changing Elasticsearch::Persistence::Model I basically had to create my own validations since I didn't see any validations other than presence. In short, you have to make a must search to make sure it doesn't exist

profiles = Profile.search query:{bool:{must:[{match:{username: "xxxxx"}}]}}
Maged Makled
  • 1,918
  • 22
  • 25