Actually I'm trying to perform a multiplication on a project.I have 2 models : a parent model and a child model.I want to show the result of the multiplication performed on the child model in the parent view by invoking the multiplication method.Here's the code:
app/models/drink.rb aka child model
class Drink < ActiveRecord::Base
belongs_to :menu
before_create :total_amount
before_save :total_amount
def total_amount
self.quantity * self.price * 1.30
end
end
> in app/models/menu.rb aka parent model
class Menu < ActiveRecord::Base
has_many :drinks, :dependent => :destroy
accepts_nested_attributes_for :drinks, :allow_destroy => true
end
in views/menu/show.html.erb
<td><%=number_to_currency(@menu.total_amount) %> </td>
and the error message:
undefined method `total_amount' for nil:NilClass
Obviously total_amount is an attribute of the model drink .What am I doing wrong.Thanks for helping.