ruby on rails - Mongoid ignores length validation on embedded document -


i have following model:

class man < user    field :surname,        type: string,  default: ""   field :name,           type: string,  default: ""    name_length = 1..50   validates :surname, length: {in: name_length}, allow_blank: true   validates :name,    length: {in: name_length}, allow_blank: true   validates :father,  length: {in: name_length}, allow_blank: true    field :birthday,       type: integer #timestamp    field :about, type: string    about_length = 2000   validates :about, length: {maximum: about_length}, allow_blank: true    embeds_many :skills   max_skills_count = 10   validates :skills, length: {maximum: max_skills_count}   #user got unique skills   index({login: 1, 'skills.caption_to_index': 1}, {unique: true, sparse: true})  end 

and embedded skills model:

class skill   include mongoid::document    caption_length = 1..50   caption_filter = /[^a-zа-я0-9]/    field :caption,          type: string   field :caption_to_index, type: string   field :order,            type: integer    embedded_in :man    validates :caption, length: {in: caption_length}   validates :order,   presence: true    before_save |document|     document.caption_to_index = document.caption.downcase.gsub(caption_filter,'')   end end 

the trouble is, mongoid ignores length validation when this:

pry(main)> u = man.find_by(login:'tester') pry(main)> s3 = skill.new(caption:'javascript',order:1) pry(main)> u.skills << s3  

i can repeat u.skills << s3 infinite times, , driver saves database immidiately.

another problem unique index not working:

index({login: 1, 'skills.caption_to_index': 1}, {unique: true, sparse: true}) 

all indexes created via rake:mongoid:create_indexes

driver allows save document same caption, here part of document in mongo shell proof:

        {                 "_id" : objectid("56a0218167e92c3b8b000000"),                 "caption" : "javascript",                 "order" : 1,                 "caption_to_index" : "javascript"         },         {                 "_id" : objectid("56a0218167e92c3b8b000000"),                 "caption" : "javascript",                 "order" : 1,                 "caption_to_index" : "javascript"         },         {                 "_id" : objectid("56a0218167e92c3b8b000000"),                 "caption" : "javascript",                 "order" : 1,                 "caption_to_index" : "javascript"         },         {                 "_id" : objectid("56a0218167e92c3b8b000000"),                 "caption" : "javascript",                 "order" : 1,                 "caption_to_index" : "javascript"         } 

test case uniqueness fails:

require 'rails_helper' include applicationhelper  describe skill   before :each     @attrs = attributes_for :tester_man     @tester = create :tester_man     expect(@tester).to be_valid     expect(@tester.persisted?).to true   end    "skill caption unique"     #debugger     s1_good = skill.new(caption:'<<cap tion ..%#',order:2)     s2_good = skill.new(caption:'other_caption',order:4)     s3_bad  = skill.new(caption:'caption',order:1)     expect(s1_good).to be_valid     expect(s2_good).to be_valid      @tester.skills = [s1_good,s2_good]     @tester.save     expect(@tester).to be_valid      s3_bad  = skill.new(caption:'caption',order:1)     @tester.skills << s3_bad     expect(@tester).not_to be_valid   end end 

how can code work expected?


Comments

Popular posts from this blog

sql - VB.NET Operand type clash: date is incompatible with int error -

SVG stroke-linecap doesn't work for circles in Firefox? -

python - TypeError: Scalar value for argument 'color' is not numeric in openCV -