I have the following mongoid model, with a scoped validation to prevent multiple votes on one bill. Each vote belongs to a user and a group:
class Vote include Mongoid::Document field :value, :type => Symbol # can be :aye, :nay, :abstain field :type, :type => Symbol # TODO can delete? belongs_to :user belongs_to :polco_group embedded_in :bill validates_uniqueness_of :value, :scope => [:polco_group_id, :user_id, :type] end
The user has the following method to add a vote to a bill:
def vote_on(bill, value) if my_groups = self.polco_groups # test to make sure the user is a member of a group my_groups.each do |g| # TODO see if already voted bill.votes.create(:value => value, :user_id => self.id, :polco_group_id => g.id, :type => g.type) end else raise "no polco_groups for this user" # #{self.full_name}" end end
and a Bill class which embeds many :votes. This is designed to allow a user to associate their vote with different groups ("Ruby Coders", "Women", etc.) and is working well, except the database currently allows a user to vote multiple times on one bill. How can I get the following to work?
u = User.last b = Bill.last u.vote_on(b,:nay) u.vote_on(b,:nay) -> should return a validation error