title: Calling Document.find with nil is invalid in mongodb
body: <p>I am using mongodb with active merchant gem in rails4. While use rails cast tutorial #145 Integrating Active Merchant. </p>
  
  <h1>Error</h1>
  
  <p>Problem: Calling Document.find with nil is invalid. Summary: Document.find expects the parameters to be 1 or more ids, and will return a single document if 1 id is provided, otherwise an array of documents if multiple ids are provided. Resolution: Most likely this is caused by passing parameters directly through to the find, and the parameter either is not present or the key from which it is accessed is incorrect.</p>
  
  <h1>Order Controller</h1>
  
  <pre><code>class OrderController &lt; ApplicationController
      include ActiveMerchant::Billing::Integrations
  
      def new
          @cart = current_cart
  
          @order = Order.new
      end
  
      def success
  
      end
  
      def failure
  
      end
  
      def create
  
          @order = current_cart.build_order(params_order)
          @order.ip_address = request.remote_ip
          if @order.save
              if @order.purchase
                  render :action =&gt; "success"
              else
                  render :action =&gt; "failure"
              end
          else
              render :action =&gt; 'new'
          end
  
      end
  
      private
  
      def current_cart 
        Cart.find(session[:cart_id])
      rescue ActiveRecord::RecordNotFound 
        cart = Cart.create 
        session[:cart_id] = cart.id 
        cart
     end
  
     def params_order
      params.require(:order).permit(:first_name, :last_name, :card_type, :card_number, :card_verification, :card_expires_on)  
     end
  
  end
  </code></pre>
  
  <h1>Model</h1>
  
  <h1>order.rb</h1>
  
  <pre><code>class Order &lt; ActiveRecord::Base
      belongs_to :cart
      has_many   :transactions, :class_name =&gt; "OrderTransaction"
  
  attr_accessor :card_number, :card_verification
  
  #validate_on_create :validate_card
  
  def purchase
      response = GATEWAY.purchase(price_in_cents, credit_card, purchase_options)
      transactions.create!(:action =&gt; "purchase", :amount =&gt; price_in_cents, :response =&gt; response)
      cart.update_attribute(:purchased_at, Time.now) if response.success?
      response.success?
  end
  
  def price_in_cents
      return 100 #(cart.total_price*100).round
  end
  
  private
  
  def purchase_options
      {
          :ip =&gt; ip_address,
          :billing_address =&gt; {
              :name     =&gt; "Ryan Bates",
              :address1 =&gt; "123 Main St.",
              :city     =&gt; "New York",
              :state    =&gt; "NY",
              :country  =&gt; "US",
              :zip      =&gt; "10001"
          }
      }
  end
  
  def validate_card
      unless credit_card.valid?
          credit_card.errors.full_messages.each do |message|
              errors.add_to_base message
          end
      end
  end
  
  def credit_card
      @credit_card ||= ActiveMerchant::Billing::CreditCard.new(
          :type               =&gt; card_type,
          :number             =&gt; card_number,
          :verification_value =&gt; card_verification,
          :month              =&gt; card_expires_on.month,
          :year               =&gt; card_expires_on.year,
          :first_name         =&gt; first_name,
          :last_name          =&gt; last_name
          )
  end
  </code></pre>
  
  <p>end</p>
  
  <h1>order_transaction.rb</h1>
  
  <pre><code>class OrderTransaction
    include Mongoid::Document
    field :order_id, type: Integer
    field :action, type: String
    field :amount, type: Integer
    field :success, type: Mongoid::Boolean
    field :authorization, type: String
    field :message, type: String
    field :params, type: String
  end
  </code></pre>
  
  <h1>cart.rb</h1>
  
  <pre><code>class Cart
    include Mongoid::Document
    has_one :order
  end
  </code></pre>
  
  <h1>Route.rb File</h1>
  
  <pre><code>get "order/new"
    get "order/success"
    get "order/failure"
    post "order/create", :to =&gt; "order#create", as: :orders
  
    resources :cart do
    resource :order
    end
  </code></pre>
  
  <h1>View</h1>
  
  <h1>new.html.haml</h1>
  
  <pre><code>%br
  %br
  %h5.text-center 
      = " Order " 
  =form_for :order, url:{action: "create"}, html:{class: "form-horizontal"} do |f|
      %div.form-group
          =f.label :first_name, 'First Name' , {:class =&gt; 'col-lg-2 control-label'}
          %div.col-lg-3
              =f.text_field :first_name, {:class =&gt; 'form-control', :placeholder =&gt; "First Name"}
      %div.form-group
          =f.label :last_name, 'Last Name', {:class =&gt; 'col-lg-2 control-label'}
          %div.col-lg-3
              =f.text_field :last_name, {:class =&gt; 'form-control', :placeholder =&gt; "Last Name"}
      %div.form-group
          =f.label :card_type, 'Card Type', {:class =&gt; 'col-lg-2 control-label'}
          %div.col-lg-3
              =f.select(:card_type, [["Visa", "visa"], ["MasterCard", "master"], ["Discover", "discover"], ["American Express", "american_express"]], {}, {:class =&gt; 'form-control', :placeholder =&gt; ""})
      %div.form-group
          =f.label :card_number, 'Card Number', {:class =&gt; 'col-lg-2 control-label'}
          %div.col-lg-3
              =f.text_field :card_number, {:class =&gt; 'form-control', :placeholder =&gt; "378282246310005"}
      %div.form-group
          =f.label :card_verification, 'Card Verification', {:class =&gt; 'col-lg-2 control-label'}
          %div.col-lg-3
              =f.text_field :card_verification, {:class =&gt; 'form-control', :placeholder =&gt; "YES"}
      %div.form-group
          =f.label :card_expires_on, 'Card Expire Date', {:class =&gt; 'col-lg-2 control-label'}
          %div.col-lg-3
              =f.date_select :card_expires_on, :discard_day =&gt; true, :start_year =&gt; Date.today.year, :end_year =&gt; (Date.today.year+10), :add_month_numbers =&gt; true
      %div.form-group
          %div.col-lg-offset-2.col-lg-10
              =f.submit :class =&gt; 'btn btn-primary'
  </code></pre>
  
  <h1>Error Occur</h1>
  
  <p><strong>Mongoid::Errors::InvalidFind in OrderController#new</strong>
  Problem: Calling Document.find with nil is invalid. Summary: Document.find expects the parameters to be 1 or more ids, and will return a single document if 1 id is provided, otherwise an array of documents if multiple ids are provided. Resolution: Most likely this is caused by passing parameters directly through to the find, and the parameter either is not present or the key from which it is accessed is incorrect.
  ** Your suggestion will helpful. Thanks in advanced**</p>