title: Ruby on Rails nil can't be coerced into BigDecimal
body: <p>Why do I get <code>nil can't be coerced into BigDecimal</code> when I try to perform a calculation: here's the code: </p>
  
  <p><strong>model/drink.rb</strong></p>
  
  <pre><code>class Drink &lt; ActiveRecord::Base
    belongs_to :menu 
    before_save :total_amount 
  
  def total_amount
      self.total_amount = self.price * self.quantity
  end 
  </code></pre>
  
  <p><strong>model/menu.rb</strong></p>
  
  <pre><code>class Menu &lt; ActiveRecord::Base
      has_many :drinks, :dependent =&gt; :destroy
      accepts_nested_attributes_for :drinks, :allow_destroy =&gt; true
      #Validations
  
  end
  </code></pre>
  
  <p>*<em>Drink is the (nested)child model and Menu the parent model</em> When I attempt to create a new drink the browser display following error message  <code>nil can't be coerced into BigDecimal  app/models/drink.rb:7:in 'total-amount'
  app/controllers/menus_controller.rb:47:in 'create'
  app/controllers/menus_controller.rb:46:in 'create'</code></p>
  
  <p>app/db/migration</p>
  
  <pre><code>class CreateDrinks &lt; ActiveRecord::Migration
    def change
      create_table :drinks do |t|
        t.string :name
        t.decimal :quantity,:precision =&gt; 8, :scale =&gt; 2
        t.decimal :price, :precision =&gt; 8, :scale =&gt; 2
        t.decimal :vat, :precision =&gt; 8, :scale =&gt; 2
        t.references :menu
  
        t.timestamps
      end
      add_index :drinks, :menu_id
    end
  end
  </code></pre>
  
  <p><strong>controllers/drinks_controller.rb</strong></p>
  
  <pre><code>   class DrinksController &lt; ApplicationController
        # GET /drinks
        # GET /drinks.json
        def index
          @drinks = Drink.all
  
          respond_to do |format|
            format.html # index.html.erb
            format.json { render :json =&gt; @drinks }
          end
        end
  
        # GET /drinks/1
        # GET /drinks/1.json
        def show
          @drink = Drink.find(params[:id])
  
          respond_to do |format|
            format.html # show.html.erb
            format.json { render :json =&gt; @drink }
          end
        end
  
        # GET /drinks/new
        # GET /drinks/new.json
        def new
          @drink = Drink.new
  
          respond_to do |format|
            format.html # new.html.erb
            format.json { render :json =&gt; @drink }
          end
        end
  
        # GET /drinks/1/edit
        def edit
          @drink = Drink.find(params[:id])
        end
  
        # POST /drinks
        # POST /drinks.json
        def create
          @article = Drink.new(params[:drink])
  
          respond_to do |format|
            if @drink.save
              format.html { redirect_to @drink, :notice =&gt; 'Drink was successfully created.' }
              format.json { render :json =&gt; @drink, :status =&gt; :created, :location =&gt; @article }
            else
              format.html { render :action =&gt; "new" }
              format.json { render :json =&gt; @drink.errors, :status =&gt; :unprocessable_entity }
            end
          end
        end
  
        # PUT /drinks/1
        # PUT /drinks/1.json
        def update
          @drink = Drink.find(params[:id])
  
          respond_to do |format|
            if @drink.update_attributes(params[:drink])
              format.html { redirect_to @drink, :notice =&gt; 'Drink was successfully updated.' }
              format.json { head :ok }
            else
              format.html { render :action =&gt; "edit" }
              format.json { render :json =&gt; @drink.errors, :status =&gt; :unprocessable_entity }
            end
          end
        end
  
        # DELETE /drinks/1
        # DELETE /drinks/1.json
        def destroy
          @drink = Drink.find(params[:id])
          @drink.destroy
  
          respond_to do |format|
            format.html { redirect_to drinks_url }
            format.json { head :ok }
          end
        end 
      end 
  </code></pre>
  
  <p>Please can anyone tell me what's wrong with the code?       </p>