Add uniqueness validation for votes

This commit is contained in:
Kaustubh Nair 2019-04-03 18:51:07 +05:30
parent b2e6c1b701
commit b9cb0804ef
2 changed files with 21 additions and 0 deletions

View file

@ -6,6 +6,8 @@ class Vote < ApplicationRecord
has_paper_trail ignore: [:updated_at], meta: { conference_id: :conference_id } has_paper_trail ignore: [:updated_at], meta: { conference_id: :conference_id }
validates :event, uniqueness: { scope: :user }
delegate :name, to: :user delegate :name, to: :user
private private

19
spec/models/vote_spec.rb Normal file
View file

@ -0,0 +1,19 @@
# frozen_string_literal: true
require 'spec_helper'
describe Vote do
let(:user) { create(:user) }
let(:event) { create(:event) }
let(:vote) { create(:vote, event: event, user: user) }
describe 'validation' do
it 'has a valid factory' do
expect(build(:vote)).to be_valid
end
it 'validates uniqueness of event in scope of user' do
expect(build(:vote, event: vote.event, user: vote.user)).not_to be_valid
end
end
end