ruby on rails - How to enforce unique embedded document in mongoid -


i have following model

class person    include mongoid::document   embeds_many :tasks end  class task   include mongoid::document   embedded_in :commit, :inverse_of => :tasks   field :name end 

how can ensure following?

person.tasks.create :name => "create facebook killer" person.tasks.create :name => "create facebook killer"  person.tasks.count == 1  different_person.tasks.create :name => "create facebook killer" person.tasks.count == 1 different_person.tasks.count == 1 

i.e. task names unique within particular person


having checked out docs on indexes thought following might work:

class person    include mongoid::document   embeds_many :tasks    index [       ["tasks.name", mongo::ascending],        ["_id", mongo::ascending]   ], :unique => true end 

but

person.tasks.create :name => "create facebook killer" person.tasks.create :name => "create facebook killer" 

still produces duplicate.


the index config shown above in person translate mongodb

db.things.ensureindex({firstname : 1, 'tasks.name' : 1}, {unique : true}) 

indexes not unique default. if @ mongo docs on this, uniqueness flag.

i don't know exact mongoid translation, you're looking this:

db.things.ensureindex({firstname : 1}, {unique : true, dropdups : true})


Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -