mirror of
https://github.com/scinote-eln/scinote-web.git
synced 2024-11-13 12:47:11 +08:00
23 lines
659 B
Ruby
23 lines
659 B
Ruby
|
class ProtocolProtocolKeyword < ActiveRecord::Base
|
||
|
after_create :increment_protocols_count
|
||
|
after_destroy :decrement_protocols_count
|
||
|
|
||
|
validates :protocol, presence: true
|
||
|
validates :protocol_keyword, presence: true
|
||
|
|
||
|
belongs_to :protocol, inverse_of: :protocol_protocol_keywords
|
||
|
belongs_to :protocol_keyword, inverse_of: :protocol_protocol_keywords
|
||
|
|
||
|
private
|
||
|
|
||
|
def increment_protocols_count
|
||
|
self.protocol_keyword.increment!(:nr_of_protocols)
|
||
|
end
|
||
|
|
||
|
def decrement_protocols_count
|
||
|
self.protocol_keyword.decrement!(:nr_of_protocols)
|
||
|
if self.protocol_keyword.nr_of_protocols == 0 then
|
||
|
self.protocol_keyword.destroy
|
||
|
end
|
||
|
end
|
||
|
end
|