add validation for name, used and quantity
This commit is contained in:
parent
4b72d383ea
commit
e73baa2754
4 changed files with 25 additions and 4 deletions
|
|
@ -1,10 +1,12 @@
|
|||
class Resource < ActiveRecord::Base
|
||||
belongs_to :conference
|
||||
validate :used_less_than_quantity
|
||||
validates :name, :used, :quantity, presence: true
|
||||
validates :used, :quantity, numericality: { greater_than_or_equal_to: 0, only_integer: true }
|
||||
validate :used_no_more_than_quantity
|
||||
|
||||
private
|
||||
|
||||
def used_less_than_quantity
|
||||
errors.add(:used, 'can not be higher than total quantity') unless used <= quantity
|
||||
def used_no_more_than_quantity
|
||||
errors.add(:used, 'cannot be higher than total quantity') if used.present? && quantity.present? && used > quantity
|
||||
end
|
||||
end
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue