1
0
This repository has been archived on 2023-12-07. You can view files and clone it, but cannot push or open issues or pull requests.
body_tracking/app/models/nutrient.rb
2019-09-15 19:55:25 +02:00

18 lines
567 B
Ruby

class Nutrient < ActiveRecord::Base
belongs_to :ingredient, inverse_of: :nutrients
belongs_to :quantity
belongs_to :unit
# disabled to avoid loop with Ingredient 'validates_associated :nutrients'
#validates :ingredient, presence: true, associated: true
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