title: how to populate belongs_to model with Faker gem
body: <p>Current schema is this: </p>
  
  <pre><code>ActiveRecord::Schema.define(version: 20160622075955) do
  
    # These are extensions that must be enabled in order to support this database
    enable_extension "plpgsql"
  
    create_table "posts", force: :cascade do |t|
      t.string   "title"
      t.string   "body"
      t.date     "date"
      t.integer  "user_id"
      t.datetime "created_at", null: false
      t.datetime "updated_at", null: false
    end
  
    add_index "posts", ["user_id"], name: "index_posts_on_user_id", using: :btree
  
    create_table "users", force: :cascade do |t|
      t.string   "first_name"
      t.string   "last_name"
      t.datetime "created_at", null: false
      t.datetime "updated_at", null: false
    end
  
    add_index "users", ["first_name"], name: "index_users_on_first_name", using: :btree
    add_index "users", ["last_name"], name: "index_users_on_last_name", using: :btree
  
    add_foreign_key "posts", "users"
  end
  </code></pre>
  
  <p>Trying to populate both models: Posts &amp; Users </p>
  
  <p>This is what I have for <code>seeds.rb</code> </p>
  
  <pre><code>20.times do |n| 
      a = User.new
      a.first_name = Faker::Name.first_name
      a.last_name = Faker::Name.last_name
  
      a.save
  
  end
  
  10.times do |n|
      b = Post.new
      b.title = Faker::Book.title
      b.body = Faker::Lorem.words(4, true)
      b.date = Faker::Time.between(DateTime.now - 1, DateTime.now) 
  
      b.save
  end
  </code></pre>
  
  <p>It works for <code>User</code> but not for <code>Post</code>. How do I also populate <code>Post</code> as well? </p>
  
  <p><strong>EDIT</strong></p>
  
  <p><strong>Post's Model</strong></p>
  
  <pre><code>class Post &lt; ActiveRecord::Base
    belongs_to :user
    validates :title, :body, presence: true
    validates :title, length: { maximum: 40 }
  end
  </code></pre>
  
  <p><strong>User's Model</strong></p>
  
  <pre><code>class User &lt; ActiveRecord::Base
      has_many :posts
      validates :first_name, :last_name, presence: true
  end
  </code></pre>
  
  <p><strong>User's Controller</strong></p>
  
  <pre><code>class UsersController &lt; ApplicationController
    def index
      @users = User.all
    end
  
    def show
      @user = User.find(params[:id])
    end
  
    def new
      @user = User.new
    end
  
    def create
      @user = User.new(user_params)
      if @user.save 
        flash[:notice] = "User successfully added!"
        redirect_to users_path
      else
        render :new
      end
    end
  
    def edit
      @user = User.find(params[:id])
      render :edit
    end
  
    def update
      @user = User.find(params[:id])
      if @user.update(user_params)
        redirect_to users_path
      else
        render :edit
      end
    end
  
    def destroy
      @user = User.find(params[:id])
      @user.destroy
        redirect_to users_path
    end
  
    private 
  
      def user_params
        params.require(:user).permit(:first_name, :last_name)
      end
  
  end
  </code></pre>
  
  <p><strong>Post's Controllers</strong></p>
  
  <pre><code>class PostsController &lt; ApplicationController
  
    def new 
      @user = User.find(params[:user_id])
      @post = @user.posts.new
    end
  
    def edit
      @user = User.find(params[:user_id])
      @post = @user.posts.find(params[:id])
      render :edit
    end
  
    def create 
      @user = User.find(params[:user_id])
      @post = @user.posts.new(post_params)
      if @post.save 
        redirect_to user_path(@post.user)
      else
        render :new
      end
    end
  
    def update 
      @user = User.find(params[:user_id])
      @post = @user.posts.find(params[:id])
      if @post.update(post_params)
        redirect_to user_path(@post.user)
      else
        render 'edit'
      end
    end
  
  
    def destroy
      @user = User.find(params[:user_id])
      @post = @user.posts.find(params[:id])
      @post.destroy
        redirect_to users_path
      end
  
    private 
  
    private 
      def post_params
        params.require(:post).permit(:title, :body, :date)
      end
  end
  </code></pre>
  
  <p><strong>EDIT TWO</strong></p>
  
  <p>No errors shows up when I put <code>puts b.errors.inspect</code> in the <code>seeds.rb</code> file. </p>
  
  <p>However, this is what shows up in the terminal when I run <code>rake db:seed</code></p>
  
  <pre><code>#&lt;ActiveModel::Errors:0x007fa20d45aec8 @base=#&lt;Post id: 1, title: "Surprised by Joy", body: "[\"sunt\", \"audentia\", \"pectus\", \"omnis\"]", date: "2016-06-26", user_id: nil, created_at: "2016-06-28 04:38:02", updated_at: "2016-06-28 04:38:02"&gt;, @messages={}&gt;
  #&lt;ActiveModel::Errors:0x007fa20d463a28 @base=#&lt;Post id: 2, title: "The Golden Bowl", body: "[\"cohibeo\", \"verus\", \"ut\", \"corrupti\"]", date: "2016-06-27", user_id: nil, created_at: "2016-06-28 04:38:02", updated_at: "2016-06-28 04:38:02"&gt;, @messages={}&gt;
  #&lt;ActiveModel::Errors:0x007fa20d450220 @base=#&lt;Post id: 3, title: "Such, Such Were the Joys", body: "[\"video\", \"tepesco\", \"stabilis\", \"esse\"]", date: "2016-06-26", user_id: nil, created_at: "2016-06-28 04:38:02", updated_at: "2016-06-28 04:38:02"&gt;, @messages={}&gt;
  #&lt;ActiveModel::Errors:0x007fa20d439d18 @base=#&lt;Post id: 4, title: "Jesting Pilate", body: "[\"aut\", \"textor\", \"tui\", \"subiungo\"]", date: "2016-06-26", user_id: nil, created_at: "2016-06-28 04:38:02", updated_at: "2016-06-28 04:38:02"&gt;, @messages={}&gt;
  #&lt;ActiveModel::Errors:0x007fa20d428888 @base=#&lt;Post id: 5, title: "Great Work of Time", body: "[\"cupiditas\", \"debeo\", \"decipio\", \"clementia\"]", date: "2016-06-27", user_id: nil, created_at: "2016-06-28 04:38:02", updated_at: "2016-06-28 04:38:02"&gt;, @messages={}&gt;
  #&lt;ActiveModel::Errors:0x007fa20d4192c0 @base=#&lt;Post id: 6, title: "Look to Windward", body: "[\"totidem\", \"numquam\", \"ut\", \"amita\"]", date: "2016-06-26", user_id: nil, created_at: "2016-06-28 04:38:02", updated_at: "2016-06-28 04:38:02"&gt;, @messages={}&gt;
  #&lt;ActiveModel::Errors:0x007fa20d409d70 @base=#&lt;Post id: 7, title: "Vile Bodies", body: "[\"cruentus\", \"adiuvo\", \"stella\", \"cimentarius\"]", date: "2016-06-26", user_id: nil, created_at: "2016-06-28 04:38:02", updated_at: "2016-06-28 04:38:02"&gt;, @messages={}&gt;
  #&lt;ActiveModel::Errors:0x007fa20d3fa6e0 @base=#&lt;Post id: 8, title: "Consider the Lilies", body: "[\"dolor\", \"sumptus\", \"solutio\", \"theologus\"]", date: "2016-06-27", user_id: nil, created_at: "2016-06-28 04:38:02", updated_at: "2016-06-28 04:38:02"&gt;, @messages={}&gt;
  #&lt;ActiveModel::Errors:0x007fa20d3eaf38 @base=#&lt;Post id: 9, title: "Terrible Swift Sword", body: "[\"aspernatur\", \"aegrus\", \"studio\", \"adstringo\"]", date: "2016-06-26", user_id: nil, created_at: "2016-06-28 04:38:02", updated_at: "2016-06-28 04:38:02"&gt;, @messages={}&gt;
  #&lt;ActiveModel::Errors:0x007fa20d3db920 @base=#&lt;Post id: nil, title: "By Grand Central Station I Sat Down and Wept", body: "[\"earum\", \"rerum\", \"defessus\", \"sed\"]", date: "2016-06-26", user_id: nil, created_at: nil, updated_at: nil&gt;, @messages={:title=&gt;["is too long (maximum is 40 characters)"]}&gt;
  </code></pre>