我有以下关联
class Employee
include Mongoid::Document
employee_id :name
references_many :companies, stored_as => :array, :inverse_of => :employees
end
class Company
include Mongoid::Document
field :name
references_many :employees, stored_as => :array, :inverse_of => :companies
end
现在如何在单个employee_id
company
的唯一性
答案 0 :(得分:0)
employee_id :name
对于验证,您应该能够这样做:
class Employee
include Mongoid::Document
field :employee_id, :type => Integer
references_many :companies, :stored_as => :array, :inverse_of => :employees
validates_uniqueness_of :employee_id
end
您可以像这样轻松地进行测试:
>> e = Employee.create :employee_id => 10
=> #
>> Employee.new(:employee_id => 10).valid?
=> false