Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6695247
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:11:54+00:00 2026-05-26T06:11:54+00:00

Actually I’m trying to perform a multiplication on a project.I have 2 models :

  • 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 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T06:11:55+00:00Added an answer on May 26, 2026 at 6:11 am

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Actually i'm trying to show and dialog into a parent form, An reference example
Actually i want to implement swipe left and right in UIScrollview. i have scrollview
Actually not sure on the terminology of what I'm trying to do. I have
Actually, this question seems to have two parts: How to implement pattern matching? How
Actually, I'm using this way. Do you have a better way? private bool AcceptJson(HttpRequest
Actually my question is all in the title. Anyway: I have a class and
Actually what i am trying to build is like a kind of firewall. It
Actually I am trying to move some box alternatively with in another box. I
Actually I have two questions. (1) Is there any reduction in processing power or
Actually, I have the task of finding the employees based on the salary rank.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.