mirror of
https://github.com/scinote-eln/scinote-web.git
synced 2024-11-11 10:06:53 +08:00
25 lines
798 B
Ruby
25 lines
798 B
Ruby
class SampleMyModule < ActiveRecord::Base
|
|
|
|
after_create :increment_nr_of_module_samples
|
|
after_destroy :decrement_nr_of_module_samples
|
|
validates :sample, :my_module, presence: true
|
|
|
|
# One sample can only be assigned once to a specific module
|
|
validates_uniqueness_of :sample_id, :scope => :my_module_id
|
|
|
|
belongs_to :assigned_by, foreign_key: 'assigned_by_id', class_name: 'User'
|
|
belongs_to :sample,
|
|
inverse_of: :sample_my_modules
|
|
belongs_to :my_module,
|
|
inverse_of: :sample_my_modules
|
|
|
|
def increment_nr_of_module_samples
|
|
my_module.increment!(:nr_of_assigned_samples)
|
|
sample.increment!(:nr_of_modules_assigned_to)
|
|
end
|
|
|
|
def decrement_nr_of_module_samples
|
|
my_module.decrement!(:nr_of_assigned_samples)
|
|
sample.decrement!(:nr_of_modules_assigned_to)
|
|
end
|
|
end
|