separation of concerns - Rails logic best practices -
i had calculations on controller this
@travel.food_expenses.map { |e| e.value * e.amount }.sum
i know shouldn't have application logic on controller, created method on travel model
def self.sum_food_expenses self.food_expenses.map { |e| e.value * e.amount }.sum end
but return me undefined method sum_food_expenses travelscontroller
as workaround put methods on applicationcontroller as:
def sum_food_expenses(travel) travel.food_expenses.map { |e| e.value * e.amount }.sum end
it works very messy, how should proceed?
on travel model, def self.sum_food_expenses
defined class method, not instance method. want def sum_food_expenses
. in controller can call @travel.sum_food_expenses
.
edit: also, not stickler, self.food_expenses.map { |e| e.value * e.amount }.sum
o(2n)
, written food_expenses.sum { |e| e.value * e.amount }
, o(n)
.
Comments
Post a Comment