title: has_one association and disappearing user id
body: <p>I'm having an error which most likely is caused by has_one associations in my model.</p>
  
  <p><strong>Issue</strong></p>
  
  <p>After declaring those associations i started having weird interaction where i'd suddenly get <code>"undefined method</code>email' for nil:NilClass"` with the following snippet from the error console:</p>
  
  <pre><code>&lt;div class="row"&gt;
  
  &lt;div class="column-md-6 column-md-offset-3"&gt;
  
  &lt;% @posts.each do |post| %&gt;
  
    email: &lt;%= post.user.email %&gt; &lt;br&gt; # &lt;= source of the issue!
  
    Level: &lt;%= post.level %&gt; &lt;br&gt;
  
    Region: &lt;%= post.region %&gt; &lt;br&gt;
  
    Info: &lt;%= post.description %&gt; &lt;br&gt;
  
    &lt;% if post.user == current_user %&gt;
  
    &lt;%= link_to "Edit", edit_post_path(post) %&gt;
  </code></pre>
  
  <p><strong>Source of the issue</strong></p>
  
  <p>I had hard time tracking down the bug and trying to reproduce it. I took a quick look at my database in rails console and found out that user_id becomes nil even though the creation of the post from user is successful. I finally managed to reproduce the error by following these steps:</p>
  
  <p>1) Login</p>
  
  <p>2) Create the post</p>
  
  <p>3) Go to "All posts". Post index action renders here all the Posts from every user correctly.</p>
  
  <p>4) While still logged in as the same user, create another post.</p>
  
  <p>5) Go back to "All posts" and I get the aforementioned error.</p>
  
  <p><strong>Post model</strong></p>
  
  <pre><code>class Post &lt; ActiveRecord::Base
    validates_presence_of :level, :region, :description
    validates :level, length: { maximum: 50 }
  
    belongs_to :user
  end
  </code></pre>
  
  <p><strong>User model</strong></p>
  
  <pre><code>class User &lt; ActiveRecord::Base  
    devise :database_authenticatable, :registerable,
           :recoverable, :rememberable, :trackable, :validatable
  
    has_one :post
  end
  </code></pre>
  
  <p><strong>Posts controller</strong></p>
  
  <pre><code>class PostsController &lt; ApplicationController
    before_action :set_post, only: [:edit, :update]
    before_action :authenticate_user!, except: [:index, :show]
  
    def index
      @posts = Post.all
    end
  
    def show
      @post = Post.find(params[:id])
    end
  
    def new
      @post = current_user.build_post
    end
  
    def create
      @post = current_user.build_post(post_params)
      if @post.save
        flash[:success] = "Post successfully created!"
        redirect_to posts_path
      else
        render 'new'
      end
    end
  
    def edit
    end
  
    def update
     #@post = Post.find(post_params)
      if @post.update(post_params)
        flash[:success] = "Profile updated"
        redirect_to posts_path
      else
        render 'edit'
      end
    end
  
    def destroy
      Post.find(params[:id]).destroy
      flash[:success] = "Post deleted"
      redirect_to posts_path
    end
  
  private
  
   def post_params
     params.require(:post).permit(:description, :level, :region)
   end
  
   def set_post
     @post = Post.find(params[:id])
   end
  </code></pre>
  
  <p>end</p>
  
  <p><strong>schema.rb</strong></p>
  
  <pre><code>ActiveRecord::Schema.define(version: 20151209193950) do
  
    create_table "posts", force: :cascade do |t|
      t.text     "description"
      t.string   "level"
      t.string   "region"
      t.datetime "created_at",  null: false
      t.datetime "updated_at",  null: false
      t.integer  "user_id"
    end
  
    create_table "users", force: :cascade do |t|
      t.string   "email",                  default: "", null: false
      t.string   "encrypted_password",     default: "", null: false
      t.string   "reset_password_token"
      t.datetime "reset_password_sent_at"
      t.datetime "remember_created_at"
      t.integer  "sign_in_count",          default: 0,  null: false
      t.datetime "current_sign_in_at"
      t.datetime "last_sign_in_at"
      t.string   "current_sign_in_ip"
      t.string   "last_sign_in_ip"
      t.datetime "created_at",                          null: false
      t.datetime "updated_at",                          null: false
    end
  
    add_index "users", ["email"], name: "index_users_on_email", unique: true
    add_index "users", ["reset_password_token"], name:      "index_users_on_reset_password_token", unique: true
  
  end
  </code></pre>
  
  <p><strong>Question</strong></p>
  
  <p>My goal was to have a one post limit for each user. I'm almost certain the has_one association is part of the issue. My question is whether it was a correct way of achieving my goal or should i use has_many association and enforce the limit in some other way?</p>
  
  <p>If has_one is the correct way, how do i fix this issue?</p>