title: Rails Association Bug
body: <p>I am getting <strong><em>NoMethodError in SitesController#index undefined method `name' for nil:NilClass</em></strong> in my Salesman index view and cannot find the culprit.</p>
  
  <p>I have a simple rails app with the following tables: Customer, Salesman and Invoice.</p>
  
  <p>In the index view for the customer table I have the following call: </p>
  
  <p><code>&lt;% @customers.each do |customer| %&gt;
      &lt;%= customer.name %&gt;&lt;/td&gt;
      &lt;%= customer.address %&gt;
      &lt;%= customer.salesman.name %&gt;
  &lt;% end %&gt;</code></p>
  
  <p>This is the call that results in the undefined method ´name´ listed above. I made sure the salesman table has a salesman_id foreign key in the customer table. Also, I made the respective association in the models: </p>
  
  <p><code>class Customer &lt; ActiveRecord::Base
      belongs_to :salesman
       has_many :invoices
  end</code></p>
  
  <p><code>class Salesman &lt; ActiveRecord::Base
      has_many :customers
  end</code></p>
  
  <p>I tested the <code>customer.salesman.name</code> call in the console and it works flawlessly. The show view for the customer has an exact call like this one and it also works. The only way I can make the customer index pass is by replacing <code>customer.salesman.name</code> to <code>customer.salesman_id</code> returning only the id.</p>
  
  <p>Thank you for your time here.</p>
  
  <pre><code>***schema.rb*** 
  
  ActiveRecord::Schema.define(version: 20150325172212) do
  create_table "customers", force: :cascade do |t|
      t.string   "name"
      t.string   "address"
      t.datetime "created_at",               null: false
      t.datetime "updated_at",               null: false
      t.integer  "salesman_id"
    end
  create_table "invoices", force: :cascade do |t|
      t.datetime "date"
      t.string   "fid"
      t.integer  "salesman_id"
      t.datetime "created_at",      null: false
      t.datetime "updated_at",      null: false
      t.string   "address"
    end
  create_table "salesmen", force: :cascade do |t|
      t.datetime "name"
      t.string   "address""
      t.datetime "created_at",      null: false
      t.datetime "updated_at",      null: false
    end 
  
  ***controllers***
  
  **customer**
  
  Class CustomersController &lt; ApplicationController
    before_action :set_customer, only: [:show, :edit, :update, :destroy]
    # GET /customers
    # GET /customers.json
    def index
      @customers = Customer.all
    end
    # GET /customers/1
    # GET /customers/1.json
    def show
    end
    # GET /customers/new
    def new
      @customer = Customer.new
    end
    # GET /customers/1/edit
    def edit
    end
    # POST /customers
    # POST /customers.json
    def create
      @customer = Customer.new(customer_params)
      respond_to do |format|
        if @customer.save
          format.html { redirect_to @customer, notice: 'Customer was successfully created.' }
          format.json { render :show, status: :created, location: @customer }
        else
          format.html { render :new }
          format.json { render json: @customer.errors, status: :unprocessable_entity }
        end
      end
    end
    # PATCH/PUT /customers/1
    # PATCH/PUT /customers/1.json
    def update
      respond_to do |format|
        if @customer.update(customer_params)
          format.html { redirect_to @customer, notice: 'Customer was successfully updated.' }
          format.json { render :show, status: :ok, location: @customer }
        else
          format.html { render :edit }
          format.json { render json: @customer.errors, status: :unprocessable_entity }
        end
      end
    end
    # DELETE /customers/1
    # DELETE /customers/1.json
    def destroy
      @customer.destroy
      respond_to do |format|
        format.html { redirect_to customers_url, notice: 'Customer was successfully destroyed.' }
        format.json { head :no_content }
      end
    end
    private
      # Use callbacks to share common setup or constraints between actions.
      def set_cliente
        @cliente = Cliente.find(params[:id])
      end
      # Never trust parameters from the scary internet, only allow the white list through.
      def customer_params
        params.require(:customer).permit(:name, :address, :salesman_id)
      end
  end
  
  ***salesman***
  
  class SalesmenController &lt; ApplicationController
    before_action :set_salesman, only: [:show, :edit, :update, :destroy]
    # GET /salesmans
    # GET /salesmans.json
    def index
      @salesmen = Salesman.all
    end
    # GET /salesmans/1
    # GET /salesmans/1.json
    def show
    end
    # GET /salesmans/new
    def new
      @salesman = Salesman.new
    end
    # GET /salesmans/1/edit
    def edit
    end
    # POST /salesmans
    # POST /salesmans.json
    def create
      @salesman = Salesman.new(salesman_params)
      respond_to do |format|
        if @salesman.save
          format.html { redirect_to @salesman, notice: 'Salesman was successfully created.' }
          format.json { render :show, status: :created, location: @salesman }
        else
          format.html { render :new }
          format.json { render json: @salesman.errors, status: :unprocessable_entity }
        end
      end
    end
    # PATCH/PUT /salesmans/1
    # PATCH/PUT /salesmans/1.json
    def update
      respond_to do |format|
        if @salesman.update(salesman_params)
          format.html { redirect_to @salesman, notice: 'Salesman was successfully updated.' }
          format.json { render :show, status: :ok, location: @salesman }
        else
          format.html { render :edit }
          format.json { render json: @salesman.errors, status: :unprocessable_entity }
        end
      end
    end
    # DELETE /salesmans/1
    # DELETE /salesmans/1.json
    def destroy
      @salesman.destroy
      respond_to do |format|
        format.html { redirect_to salesmans_url, notice: 'Salesman was successfully destroyed.' }
        format.json { head :no_content }
      end
    end
    private
      # Use callbacks to share common setup or constraints between actions.
      def set_salesman
        @salesman = Salesman.find(params[:id])
      end
      # Never trust parameters from the scary internet, only allow the white list through.
      def salesman_params
        params.require(:salesman).permit(:name, :address)
      end
  
  **invoice**
  
  class InvoicesController &lt; ApplicationController
    before_action :set_invoice, only: [:show, :edit, :update, :destroy]
    # GET /invoices
    # GET /invoices.json
    def index
      @invoices = Invoice.all
    end
    # GET /invoices/1
    # GET /invoices/1.json
    def show
      @invoice = Invoice.find(params[:id])
      @ordenes = @invoice.ordenes
    end
    # GET /invoices/new
    def new
      @invoice = Invoice.new
    end
    # GET /invoices/1/edit
    def edit
    end
    # POST /invoices
    # POST /invoices.json
    def create
      @invoice = Invoice.new(invoice_params)
      respond_to do |format|
        if @invoice.save
          format.html { redirect_to @invoice, notice: 'Invoice was successfully created.' }
          format.json { render :show, status: :created, location: @invoice }
        else
          format.html { render :new }
          format.json { render json: @invoice.errors, status: :unprocessable_entity }
        end
      end
    end
    # PATCH/PUT /invoices/1
    # PATCH/PUT /invoices/1.json
    def update
      respond_to do |format|
        if @invoice.update(invoice_params)
          format.html { redirect_to @invoice, notice: 'Invoice was successfully updated.' }
          format.json { render :show, status: :ok, location: @invoice }
        else
          format.html { render :edit }
          format.json { render json: @invoice.errors, status: :unprocessable_entity }
        end
      end
    end
    # DELETE /invoices/1
    # DELETE /invoices/1.json
    def destroy
      @invoice.destroy
      respond_to do |format|
        format.html { redirect_to invoices_url, notice: 'Invoice was successfully destroyed.' }
        format.json { head :no_content }
      end
    end
    private
      # Use callbacks to share common setup or constraints between actions.
      def set_invoice
        @invoice = Invoice.find(params[:id])
      end
      # Never trust parameters from the scary internet, only allow the white list through.
      def invoice_params
        params.require(:invoice).permit(:date, :fid, :name, :address, :salesman_id)
      end
  end
  </code></pre>
  
  <p>-davefogo</p>