I have a model that is something similar to the following
class Lecture
include Mongoid::Document
belongs_to :orgnization
belongs_to :schedule
has_one :lecturer
validates :lecturer, presence: true, uniqueness: { scope: [:orgnization, :schedule] }
end
this works perfectly fine validating that the lecturer is unique per schedule per orgnization...
the problem rises when I try to make the lecture has_many :lecturers
class Lecture
include Mongoid::Document
belongs_to :orgnization
belongs_to :schedule
has_many :lecturers
# the following validation doesn't work
validates :lecturers, presence: true, uniqueness: { scope: [:orgnization, :schedule] }
end
how do I fix this so that it evaluates the uniqueness of the has_many
the same way it evaluates the has_one
relation
I'd like to have something like the following
class Lecture
...
validate :lecturers_schedule
def lecturers_schedule
# Pseudo code
lecturers.each do |lecturer|
validates :lecturer, uniqueness: { scope: [:orgnization, :schedule] }
end
end
end
I've looked at this answer but it didn't work