title: Rails display value from lookup table in view
body: <p>Newbie alert! I hope I can explain my situation accurately and understandably.</p>
  
  <p>I have a model called animals. Another model called statuses. Another called sexes.</p>
  
  <p>From animal.rb model:</p>
  
  <pre><code>class Animal &lt; ActiveRecord::Base
      belongs_to :status, :class_name =&gt; Status, :foreign_key =&gt; :status_id
      belongs_to :sex, :class_name =&gt; Sex, :foreign_key =&gt; :sex_id
  end
  </code></pre>
  
  <p>sex.rb model:</p>
  
  <pre><code>class Sex &lt; ActiveRecord::Base
      has_many :animals
  end
  </code></pre>
  
  <p>status.rb model:</p>
  
  <pre><code>class Status &lt; ActiveRecord::Base
      has_many :animals
  end
  </code></pre>
  
  <p>In my views/animals/index.html.erb I have this:</p>
  
  <pre><code>&lt;% @animals.each do |animal| %&gt;
        &lt;tr&gt;
          &lt;td&gt;&lt;%= animal.name %&gt;&lt;/td&gt;
          &lt;td&gt;&lt;%= animal.eartag %&gt;&lt;/td&gt;
          &lt;td&gt;&lt;%= animal.sex.sex %&gt;&lt;/td&gt;
          &lt;td&gt;&lt;%= animal.status.status %&gt;&lt;/td&gt;
  ...
  </code></pre>
  
  <p>The column in the table displays the sex from the sexes lookup table fine, but the line for status causes the following error:</p>
  
  <p>NoMethodError in Animals#index 
  undefined method `status' for nil:NilClass</p>
  
  <p>So I guess Rails is thinking animal.status is nil?? I don't know why it would be when animal.sex is not nil.</p>
  
  <p>Also, I have views/animals/show.html.erb. Part of that is:</p>
  
  <pre><code>  &lt;dt&gt;&lt;strong&gt;&lt;%= model_class.human_attribute_name(:sex_id) %&gt;:&lt;/strong&gt;&lt;/dt&gt;
    &lt;dd&gt;&lt;%= @animal.sex.sex %&gt;&lt;/dd&gt;
   &lt;dt&gt;&lt;strong&gt;&lt;%= model_class.human_attribute_name(:status_id) %&gt;:&lt;/strong&gt;&lt;/dt&gt;
    &lt;dd&gt;&lt;%= @animal.status.status %&gt;&lt;/dd&gt;
  </code></pre>
  
  <p>And this works fine. I.e. in the columns for Sex and Status, it displays the values from the sex and status columns of the sex and status tables, respectively, as I want the index.html.erb view to do.</p>
  
  <p>I don't know if it makes any difference, but here's animals_controller.rb:</p>
  
  <pre><code>class AnimalsController &lt; ApplicationController
    before_action :set_animal, only: [:show, :edit, :update, :destroy]
  
    # GET /animals
    # GET /animals.json
    def index
      @animals = Animal.all
    end
  
    # GET /animals/1
    # GET /animals/1.json
    def show
    end
  
    # GET /animals/new
    def new
      @animal = Animal.new
    end
  
    # GET /animals/1/edit
    def edit
    end
  
    # POST /animals
    # POST /animals.json
    def create
      @animal = Animal.new(animal_params)
  
      respond_to do |format|
        if @animal.save
          format.html { redirect_to @animal, notice: 'Animal was successfully created.' }
          format.json { render action: 'show', status: :created, location: @animal }
        else
          format.html { render action: 'new' }
          format.json { render json: @animal.errors, status: :unprocessable_entity }
        end
      end
    end
  
    # PATCH/PUT /animals/1
    # PATCH/PUT /animals/1.json
    def update
      respond_to do |format|
        if @animal.update(animal_params)
          format.html { redirect_to @animal, notice: 'Animal was successfully updated.' }
          format.json { head :no_content }
        else
          format.html { render action: 'edit' }
          format.json { render json: @animal.errors, status: :unprocessable_entity }
        end
      end
    end
  
    # DELETE /animals/1
    # DELETE /animals/1.json
    def destroy
      @animal.destroy
      respond_to do |format|
        format.html { redirect_to animals_url }
        format.json { head :no_content }
      end
    end
  
    private
      # Use callbacks to share common setup or constraints between actions.
      def set_animal
        @animal = Animal.find(params[:id])
      end
  
      # Never trust parameters from the scary internet, only allow the white list through.
      def animal_params
        params.require(:animal).permit(:name, :eartag, :reg_num, :sex_id, :date_birth, :date_acquired, :date_removed, :status_id, :num_horns, :sire_id, :dam_id, :source_id, :origin_id, :percent_black, :percent_lilac, :for_sale, :for_sale_status_id, :quality_id, :sale_price, :to_be_culled, :comments, :comments_web, :show_on_website, :core_flock, :birth_id, :rejected_at_birth)
      end
  end
  </code></pre>
  
  <p>I hope this makes some kind of sense. Thanks!</p>