Added Ingredient/Nutrient initialization
This commit is contained in:
@@ -24,4 +24,12 @@ class Ingredient < ActiveRecord::Base
|
||||
validates :ref_amount, numericality: {greater_than: 0}
|
||||
validates :ref_unit, presence: true, associated: true
|
||||
validates :group, inclusion: {in: groups.keys}
|
||||
|
||||
after_initialize do
|
||||
if new_record?
|
||||
units = self.project.units
|
||||
self.ref_unit ||= units.find_by(shortname: 'g') || units.first
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -8,4 +8,10 @@ class Nutrient < ActiveRecord::Base
|
||||
validates :quantity, presence: true, associated: true, uniqueness: {scope: :ingredient_id}
|
||||
validates :amount, numericality: {greater_than: 0}
|
||||
validates :unit, presence: true, associated: true
|
||||
|
||||
after_initialize do
|
||||
if new_record?
|
||||
self.unit ||= self.ingredient.ref_unit
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user