handle chip and chip values operations
This commit is contained in:
23
app/models/chip.rb
Normal file
23
app/models/chip.rb
Normal file
@@ -0,0 +1,23 @@
|
||||
class Chip < ApplicationRecord
|
||||
has_many :base_chip_values, class_name: "ChipValue", foreign_key: "base_chip_id", dependent: :delete_all
|
||||
has_many :secondary_chip_values, class_name: "ChipValue", foreign_key: "secondary_chip_id", dependent: :delete_all
|
||||
|
||||
validates :name, uniqueness: true
|
||||
validates :name, :symbol, presence: true
|
||||
|
||||
after_create :add_chip_values
|
||||
|
||||
private
|
||||
|
||||
# When new chip(currency) is added, add new record to the chip_values table for every existing chip(currency)
|
||||
def add_chip_values
|
||||
Chip.all.each do |chip|
|
||||
next if chip.name == name
|
||||
|
||||
transaction do
|
||||
ChipValue.new(base_chip: self, secondary_chip: chip, value: 0).save
|
||||
ChipValue.new(base_chip: chip, secondary_chip: self, value: 0).save
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user