이 헬퍼 방법 : 지방, 탄수화물, 단백질에 대한건조까지 내가 세 가지 방법이 같이있어
def total_fat
total = 0
meal_foods = current_user.meal_foods
meal_foods.each do |food|
total += food.fat
end
return total
end
하나.
DRY하고 싶습니다.
나는이 방법을 시도했지만 문자열로 '매크로'를 전달하여 작동하지 않는 것 같습니다.
def total_of(macro)
total = 0
meal_foods = current_user.meal_foods
meal_foods.each do |food|
total += food.macro
end
return total
end
어떻게하면됩니까?
'배열 #의 sum'은 ActiveSupport가 필요합니다. 질문에는 Rails 태그가 있지만 질문 자체는 순수한 루비 태그 일 수 있습니다. –
설명해 주셔서 감사합니다. – Fon