From 69f4771ec6ae086833d91407f0ccdd315612ede6 Mon Sep 17 00:00:00 2001 From: Nishanth Vijayan Date: Sat, 28 May 2016 20:57:25 +0530 Subject: [PATCH] Allow updating currency of ticket if its the only ticket --- app/models/ticket.rb | 1 + spec/models/ticket_spec.rb | 5 +++++ 2 files changed, 6 insertions(+) diff --git a/app/models/ticket.rb b/app/models/ticket.rb index 54f5c3d7..b6898981 100644 --- a/app/models/ticket.rb +++ b/app/models/ticket.rb @@ -59,6 +59,7 @@ class Ticket < ActiveRecord::Base private def tickets_of_conference_have_same_currency + return if self.persisted? && Ticket.where(conference_id: conference_id).count == 1 unless Ticket.where(conference_id: conference_id).all?{|t| t.price_currency == self.price_currency } errors.add(:price_currency, 'is different from the existing tickets of this conference.') end diff --git a/spec/models/ticket_spec.rb b/spec/models/ticket_spec.rb index fc67ff3d..714792fb 100644 --- a/spec/models/ticket_spec.rb +++ b/spec/models/ticket_spec.rb @@ -45,6 +45,11 @@ describe Ticket do end end + it 'allows updating currency of ticket if its the only ticket of the conference' do + ticket.price_currency = 'INR' + expect(ticket).to be_valid + end + describe 'association' do it { should belong_to(:conference) } it { should have_many(:ticket_purchases).dependent(:destroy) }