質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Q&A

0回答

692閲覧

【Rails 】seeds.rbの、「バリデーションに失敗しました」のエラー解決方法

ckr

総合スコア23

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

0グッド

1クリップ

投稿2020/04/20 06:01

編集2022/01/12 10:55

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

恐れ入りますが、何卒宜しくお願い致します。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問