class Meal < ApplicationRecord
	has_many :meal_portions, :dependent => :destroy
	accepts_nested_attributes_for :meal_portions, :allow_destroy => true
	has_many :portions, through: :meal_portions

	belongs_to :plan, optional: true

	default_scope { order(number: :asc) }

	attr_accessor :portions_selector

	def index
		# scope order default by number asc
		return self.plan.meals.index(self)
	end
  
	def proteins
		total = 0.0
		meal_portions.each { |mp| total += mp.proteins } 
		return total.round(2)
	end

	def fats
		total = 0.0
		meal_portions.each { |mp| total += mp.fats } 
		return total.round(2)
	end

	def carbs
		total = 0.0
		meal_portions.each { |mp| total += mp.carbs } 
		return total.round(2)
	end

	def cals
		total = 0.0
		meal_portions.each { |mp| total += mp.calories } 
		return total.round(2)
	end

	def calories
		cals
	end

end