0

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.

1
  • 1
    Have you tried @menu.drinks.total_amount ? Commented Oct 14, 2011 at 23:37

3 Answers 3

2

Obviously total_amount is an attribute of the model drink .

Yeah, that method is in the drink model (by the way, if it is an attribute then it is declared in your migration. The way you have it set up the total_amount will not be saved in the database), but, you are calling it on an instance of Menu.

As Misha M said, @menu.drinks.total_amount.

Sign up to request clarification or add additional context in comments.

4 Comments

When I put @menu.drinks.total_amount, I get a message error: undefined method 'total_amount' for <ActiveRecord::Relation:0x10318aae8>, please can you tell me what's wrong.Is it a migration issue?
Ok, I didn't get what you were trying to do until I read your response to Mitch. Ok, if you want the total_amount for a specific drink then you need a call that method on a specific drink. @menu.drinks returns all of the drink on that menu. Instead, you want something like @menu.drinks[0], which will return the first drink in the @menu.drinks relation.
@menu.drinks[0] displays #<Drink:0x1035f51f0> on view. which is the id of instance of drink (I guess?).That's great, but more than that I want each instance of drink perform total_amount(which is a method of model/drink.rb and an attribute of it). So How can I simply perform a child model calculation method in a parent model view(the method is an attribute of the child model too)?
As I said, call the method on the instance. @menu.drinks[0].total_amount. If you want more than one drink to do this then you have to loop through all the drinks and call the method on each one.
1

You need to add a function called total_amount to your Menu class, and then have it iterate through all the drinks, and sum the total amount of each drink.

1 Comment

matter fact I don't want to sum up the total_amount of each drink.total_amount is the total of one single drink price not the sum of all drink
1

Matter fact the problem is that the quantity and the price of the drink was not set.A part of the solution comes from Wizard of Ogz.Matter fact by trying to solve a "nil can't be coerced into BigDecimal" I get the solution to this issue too.So here's is the solution

1-app/models/drink.rb aka child model Apply to self.quantity and self.price a method which convert them to string(to_s) then to big decimal(to_d)

class Drink < ActiveRecord::Base
  belongs_to :menu 
  before_save :total_amount 

  def total_amount
    self.quantity.to_s.to_d * self.price.to_s.to_d * 1.30
  end   
end

2-app/models/drink.rb aka child model validates the presence of price and quantity before saving them to the database

class Drink < ActiveRecord::Base
      belongs_to :menu 
      before_save :total_amount

      validates :price, :presence => true 
      validates :quantity, :presence => true 

      def total_amount
        self.quantity.to_s.to_d * self.price.to_s.to_d * 1.30
      end   
end

3-app/views/menus/show.html.erb aka parent model

Simply apply the method total_amount to dink aka child(nested) model as following:

<td><%=number_to_currency(drink.total_amount) %> </td>

Thanks to Wizard of Ogz, Mitch Dempsey, sosborn, and Misha M

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.