diff --git a/lib/rating/models/rating/rating.rb b/lib/rating/models/rating/rating.rb index ec346da..cc5aa77 100644 --- a/lib/rating/models/rating/rating.rb +++ b/lib/rating/models/rating/rating.rb @@ -10,6 +10,11 @@ module Rating validates :average, :estimate, :resource, :sum, :total, presence: true validates :average, :estimate, :sum, :total, numericality: true + validates :resource_id, uniqueness: { + case_sensitive: false, + scope: %i[resource_type scopeable_id scopeable_type] + } + class << self def averager_data(resource, scopeable) total_count = how_many_resource_received_votes_sql?(distinct: false, scopeable: scopeable) diff --git a/spec/factories/rating/rating.rb b/spec/factories/rating/rating.rb index 920540d..250bb15 100644 --- a/spec/factories/rating/rating.rb +++ b/spec/factories/rating/rating.rb @@ -7,6 +7,6 @@ FactoryBot.define do sum 100 total 1 - resource { create :article } + association :resource, factory: :article, strategy: :build end end diff --git a/spec/models/rating_spec.rb b/spec/models/rating_spec.rb index 3976cbf..eeaf788 100644 --- a/spec/models/rating_spec.rb +++ b/spec/models/rating_spec.rb @@ -15,4 +15,10 @@ RSpec.describe Rating::Rating do it { is_expected.to validate_presence_of :resource } it { is_expected.to validate_presence_of :sum } it { is_expected.to validate_presence_of :total } + + it do + expect(object).to validate_uniqueness_of(:resource_id) + .scoped_to(%i[resource_type scopeable_id scopeable_type]) + .case_insensitive + end end