title: Rails 4: NoMethodError in <Model>Controller#index
body: <p>In our Rails 4 app, there are four models:</p>
  
  <pre><code>class User &lt; ActiveRecord::Base
    has_many :administrations, dependent: :destroy
    has_many :calendars, through: :administrations
  end
  
  class Administration &lt; ActiveRecord::Base
    belongs_to :user
    belongs_to :calendar
  end
  
  class Calendar &lt; ActiveRecord::Base
    has_many :administrations, dependent: :destroy
    has_many :users, through: :administrations
  end
  </code></pre>
  
  <p>Here is our <code>AdministrationsController</code>: </p>
  
  <pre><code>class AdministrationsController &lt; ApplicationController
  
    def to_s
      role
    end
  
    def index
      @user = current_user
      @administrations = @user.administrations
      @calendar = Calendar.new
    end
  
    def show
      @administration = Administration.find(params[:id])
    end
  
    def destroy
      Administration.find(params[:id]).destroy
      flash[:success] = "You successfully quit this calendar"
      redirect_to dashboard_path
    end
  
  end
  </code></pre>
  
  <p>When a new <code>user</code> signs up, ie: a new @user is created, he is redirected to his <code>dashboard</code>, which corresponds to the <code>Administration</code> <code>index.html.erb</code> view:</p>
  
  <pre><code>&lt;% provide(:title, 'Dashboard') %&gt;
  
  &lt;h1&gt;Dashboard&lt;/h1&gt;
  
  &lt;div class="container-fluid"&gt;
  
      &lt;div class="row"&gt;
  
          &lt;div class="col-md-4 col-md-offset-4"&gt;
              &lt;% if @user.administrations.any? %&gt;
                  &lt;h2&gt;My Calendars&lt;/h2&gt;
                  &lt;table&gt;
                      &lt;%= render @administrations %&gt;
                  &lt;/table&gt;
              &lt;% end %&gt;
  
              &lt;section class="calendar_form"&gt;
                  &lt;%= render 'shared/calendar_form' %&gt;
              &lt;/section&gt;
          &lt;/div&gt;
  
      &lt;/div&gt;
  
  &lt;/div&gt;
  </code></pre>
  
  <p>At this moment, a user has no calendar and no administration.</p>
  
  <p>So, logically, after we sign up a new user, we get the following error:</p>
  
  <pre><code>NoMethodError in AdministrationsController#index
  
  undefined method `administrations' for nil:NilClass
  
  def index
    @user = current_user
    @administrations = @user.administrations
    @calendar = Calendar.new
  end
  </code></pre>
  
  <p>We tried to do something like:</p>
  
  <pre><code>def index
    @user = current_user
    @administrations = @user.administrations if @user.administrations.exists?
    @calendar = Calendar.new
  end
  </code></pre>
  
  <p>but it did not work, and returned a similar error.</p>
  
  <p>How can we fix this?</p>