Rails を用いたWebアプリを開発しており、投稿のサンプルデータを作りたく、fakerをbundle installした後
seeds.rbにデータを入力した後、rails db:seedを行うとタイトルのエラーが発生している状態です。
・行ったこと
userモデルとpostモデルは、1対多の関係の為、モデルのバリデーションをコメントアウトしました。
また、fakerではなく、手作業でサンプルデータが登録できるかも試しましたが、
こちらも全く同じエラーとなってしまいました。
post.rb
1class Post < ApplicationRecord 2 validates :title, presence: true, uniqueness: { scope: :user_id } 3 validates :contents, presence: true, uniqueness: { scope: :user_id } 4 #belongs_to :user 5(エラー解決には至りませんでしたがbelongs_to :userをコメントアウトしてみました。) 6end 7
user.rb
1class User < ApplicationRecord 2 #has_many :posts, dependent: :destroy 3(エラー解決には至りませんでしたがhas_many :postsはコメントアウト済みです。) 4 has_many :chat_messages,dependent: :destroy 5 has_many :chat_rooms 6 7 devise :database_authenticatable, :registerable, 8 :recoverable, :rememberable, :validatable 9 validates :name, presence: true, length: { maximum: 20 } 10 validates :email, presence: true, length: { maximum: 50 } 11 validates :profile_photo, presence: true 12 13 mount_uploader :profile_photo, ProfilePhotoUploader 14 15 enum role: { user: 0, company: 1 } 16 17 def update_without_current_password(params, *options) 18 params.delete(:current_password) 19 20 if params[:password].blank? && params[:password_confirmation].blank? 21 params.delete(:password) 22 params.delete(:password_confirmation) 23 end 24 25 result = update_attributes(params, *options) 26 clean_up_passwords 27 result 28 end 29 30end 31
seeds.rb
1#20.times do 2 #Post.create( 3 #title: Faker::Job.field, 4 #contents: Faker::Twitter.status 5 #user_id: Faker::Number.between(1,20), 6 7#end 8 9users = User.order(:created_at).take(3) 1020.times do 11 contents = Faker::Lorem.sentence(5) 12 title= Faker::Job.field 13 users.each { |user| user.posts.create!(contents: content) } 14#end 15 16Post.create!( 17 [ 18 { 19 id: 1, user_id: 1, title:test1, contents:testtesttest 20 }, 21 { 22 id: 2, user_id: 2, title:test1, contents:testtesttest 23 } 24 ] 25)
schema.rb
1create_table "posts", force: :cascade do |t| 2 t.text "contents" 3 t.datetime "created_at", null: false 4 t.datetime "updated_at", null: false 5 t.text "title" 6 t.integer "user_id" 7 t.index ["user_id"], name: "index_posts_on_user_id" 8 end 9 10 create_table "users", force: :cascade do |t| 11 t.string "email", default: "", null: false 12 t.string "encrypted_password", default: "", null: false 13 t.string "reset_password_token" 14 t.datetime "reset_password_sent_at" 15 t.datetime "remember_created_at" 16 t.datetime "created_at", null: false 17 t.datetime "updated_at", null: false 18 t.string "name", default: "", null: false 19 t.string "profile_photo" 20 t.string "age" 21 t.string "sex" 22 t.text "profile" 23 t.integer "role" 24 t.index ["email"], name: "index_users_on_email", unique: true 25 t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true 26 end
恐れ入りますが、何卒宜しくお願い致します。
あなたの回答
tips
プレビュー