Reputation: 1566
Current schema is this:
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
Trying to populate both models: Posts & Users
This is what I have for seeds.rb
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
It works for User
but not for Post
. How do I also populate Post
as well?
EDIT
Post's Model
class Post < ActiveRecord::Base
belongs_to :user
validates :title, :body, presence: true
validates :title, length: { maximum: 40 }
end
User's Model
class User < ActiveRecord::Base
has_many :posts
validates :first_name, :last_name, presence: true
end
User's Controller
class UsersController < 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
Post's Controllers
class PostsController < 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
EDIT TWO
No errors shows up when I put puts b.errors.inspect
in the seeds.rb
file.
However, this is what shows up in the terminal when I run rake db:seed
#<ActiveModel::Errors:0x007fa20d45aec8 @base=#<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">, @messages={}>
#<ActiveModel::Errors:0x007fa20d463a28 @base=#<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">, @messages={}>
#<ActiveModel::Errors:0x007fa20d450220 @base=#<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">, @messages={}>
#<ActiveModel::Errors:0x007fa20d439d18 @base=#<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">, @messages={}>
#<ActiveModel::Errors:0x007fa20d428888 @base=#<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">, @messages={}>
#<ActiveModel::Errors:0x007fa20d4192c0 @base=#<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">, @messages={}>
#<ActiveModel::Errors:0x007fa20d409d70 @base=#<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">, @messages={}>
#<ActiveModel::Errors:0x007fa20d3fa6e0 @base=#<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">, @messages={}>
#<ActiveModel::Errors:0x007fa20d3eaf38 @base=#<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">, @messages={}>
#<ActiveModel::Errors:0x007fa20d3db920 @base=#<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>, @messages={:title=>["is too long (maximum is 40 characters)"]}>
Upvotes: 1
Views: 1764
Reputation: 959
The error is that the Faker
some times are creating titles that are bigger than 40 characters. (look that in the example above, you've created 9 posts (because they have an ID) and only one failed (with the error message at the end)
@messages={:title=>["is too long (maximum is 40 characters)"]}
If you wanna use the Faker, you can cut the title.
b.title = Faker::Book.title[0..40]
# it will get only the first 40 characters
This way you'll create 20 users with 1 post for each one. You can add another 5.times do ... end wrapping the Post creation so you'll have 5 post for each user. You can also create only one user and then set all post.user = User.first, so you'll have one user with lots of Posts.
20.times do
user = User.new
user.first_name = Faker::Name.first_name
user.last_name = Faker::Name.last_name
user.save
post = Post.new
post.title = Faker::Book.title[0..40]
post.body = Faker::Lorem.words(4, true)
post.date = Faker::Time.between(DateTime.now - 1, DateTime.now)
post.user = user
post.save
end
Upvotes: 1