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

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

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

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails

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

データベース

データベースとは、データの集合体を指します。また、そのデータの集合体の共用を可能にするシステムの意味を含めます

AWS(Amazon Web Services)

Amazon Web Services (AWS)は、仮想空間を機軸とした、クラスター状のコンピュータ・ネットワーク・データベース・ストーレッジ・サポートツールをAWSというインフラから提供する商用サービスです。

Q&A

解決済

2回答

878閲覧

railsでadd_commentsに失敗

sk48801728

総合スコア12

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails

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

データベース

データベースとは、データの集合体を指します。また、そのデータの集合体の共用を可能にするシステムの意味を含めます

AWS(Amazon Web Services)

Amazon Web Services (AWS)は、仮想空間を機軸とした、クラスター状のコンピュータ・ネットワーク・データベース・ストーレッジ・サポートツールをAWSというインフラから提供する商用サービスです。

0グッド

0クリップ

投稿2020/06/01 10:53

前提・実現したいこと

rails: 5.2.4.2
ruby 2.5.1

の環境にて、rails db:migrate RAILS_ENV=productionをすると下記のエラーが出てしまいます。
何か原因をご存知の方いらっしゃいましたら、ご教授頂ければと思います。

発生している問題・エラーメッセージ

== 20200514091501 AddTextToComments: migrating ================================ -- add_column(:comments, :text) rails aborted! StandardError: An error has occurred, all later migrations canceled: wrong number of arguments (given 2, expected 3..4) /var/www/social-engineer-tree/db/migrate/20200514091501_add_text_to_comments.rb:3:in `change' /var/www/social-engineer-tree/bin/rails:9:in `<top (required)>' /var/www/social-engineer-tree/bin/spring:15:in `<top (required)>' bin/rails:3:in `load' bin/rails:3:in `<main>' Caused by: ArgumentError: wrong number of arguments (given 2, expected 3..4) /var/www/social-engineer-tree/db/migrate/20200514091501_add_text_to_comments.rb:3:in `change' /var/www/social-engineer-tree/bin/rails:9:in `<top (required)>' /var/www/social-engineer-tree/bin/spring:15:in `<top (required)>' bin/rails:3:in `load' bin/rails:3:in `<main>' Tasks: TOP => db:migrate (See full trace by running task with --trace)

該当のソースコード

20200514091501_add_text_to_comments.rb

class AddTextToComments < ActiveRecord::Migration[5.2] def change add_column :comments, :text end end

試したこと

class AddTextToComments < ActiveRecord::Migration[5.2]
def change
add_column :comments, :text, :text
end
end
⬇︎
class AddTextToComments < ActiveRecord::Migration[5.2]
def change
add_column :comments, :text
end
end
へ変更しローカル環境でpull後にproduction環境でもpull済み

補足情報(FW/ツールのバージョンなど)

ここにより詳細な情報を記載してください。

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

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

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

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

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

guest

回答2

0

自己解決

unicorn.rb内の記述ミスでした

投稿2020/06/07 11:25

sk48801728

総合スコア12

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

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

0

add_column :comments, :text

は、text というcolumnを追加したいということでしょうか。
そのtextの型指定が無いです。
add_column :comments, :text,:text
とかが必要。
名前が text だから型も text になるとは思わないで。

え?!
add_column :comments, :text, :text だったのをなおしたの?
直す前はどういうエラーですか?

追記
Duplicate column name 'text': ALTER TABLE comments ADD text text
とあります。既にあるよと言われてます。
db/schema.rb確認してみてください

投稿2020/06/01 11:52

編集2020/06/02 01:26
winterboum

総合スコア23347

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

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

sk48801728

2020/06/02 02:10 編集

ご回答有難う御座います! エラー文の箇所を間違えました! 直す直前は以下エラーでした! == 20200514091501 AddTextToComments: migrating ================================ -- add_column(:comments, :text) rails aborted! StandardError: An error has occurred, all later migrations canceled: wrong number of arguments (given 2, expected 3..4) /var/www/social-engineer-tree/db/migrate/20200514091501_add_text_to_comments.rb:3:in `change' /var/www/social-engineer-tree/bin/rails:9:in `<top (required)>' /var/www/social-engineer-tree/bin/spring:15:in `<top (required)>' bin/rails:3:in `load' bin/rails:3:in `<main>' Caused by: ArgumentError: wrong number of arguments (given 2, expected 3..4) /var/www/social-engineer-tree/db/migrate/20200514091501_add_text_to_comments.rb:3:in `change' /var/www/social-engineer-tree/bin/rails:9:in `<top (required)>' /var/www/social-engineer-tree/bin/spring:15:in `<top (required)>' bin/rails:3:in `load' bin/rails:3:in `<main>' Tasks: TOP => db:migrate (See full trace by running task with --trace) ※ちなみにdb/schema.rbを確認すると ActiveRecord::Schema.define(version: 2020_05_14_091501) do create_table "comments", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t| t.integer "user_id" t.integer "tweet_id" t.datetime "created_at", null: false t.datetime "updated_at", null: false t.text "text" end と記述があり、すでにtextカラムが追加されているようですが、 rails db:migrate RAILS_ENV=productionのコマンドを打ち込んだ際にエラーが出るのはなぜでしょうか?
sk48801728

2020/06/02 02:13

bundle exec unicorn_rails -c config/unicorn.rb -E production -Dを入力すると以下エラーも出ております。 bundler: failed to load command: unicorn_rails (/home/ec2-user/.rbenv/versions/2.5.1/bin/unicorn_rails) Errno::ENOENT: No such file or directory @ rb_sysopen - config/unicorn.rb /home/ec2-user/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/unicorn-5.4.1/lib/unicorn/configurator.rb:84:in `read' /home/ec2-user/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/unicorn-5.4.1/lib/unicorn/configurator.rb:84:in `reload' /home/ec2-user/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/unicorn-5.4.1/lib/unicorn/configurator.rb:77:in `initialize' /home/ec2-user/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/unicorn-5.4.1/lib/unicorn/http_server.rb:77:in `new' /home/ec2-user/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/unicorn-5.4.1/lib/unicorn/http_server.rb:77:in `initialize' /home/ec2-user/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/unicorn-5.4.1/bin/unicorn_rails:209:in `new' /home/ec2-user/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/unicorn-5.4.1/bin/unicorn_rails:209:in `<top (required)>' /home/ec2-user/.rbenv/versions/2.5.1/bin/unicorn_rails:23:in `load' /home/ec2-user/.rbenv/versions/2.5.1/bin/unicorn_rails:23:in `<top (required)>' master failed to start, check stderr log for details
winterboum

2020/06/02 02:47

それはまた別のエラーですね。一つづつ片付けましょう。 schema.rbは確認できましたか?
winterboum

2020/06/02 02:49

あ、みおとしてた。エラーの最初の方しか見てなかった。textはあるのですね。 そうしますと、失敗したmigrationは不要なので削除しておいてください。 残っているとあとで障害の元です
sk48801728

2020/06/02 06:39 編集

add_column :comments, :text,:text 上記コードを削除し、コミット、pullを行いました。 その後再度rails db:migrate RAILS_ENV=productionを打ち込んだところ、 特に何も表示はなくコマンド入力待ちとなりました。 その後、再度add_column :comments, :text,:textを記述しrails db:migrate RAILS_ENV=productionを打ち込んだところ同様にエラーは吐かずコマンド入力待ちとなりました。
winterboum

2020/06/02 07:18

はて、、、 既に test がある、というメッセージだったのにまた追加した? でそれは通った?  念の為 1) shcema.rb 見ておいてください。希望通りの内容かどうか 2) RAILS_ENV=production rails db:migrate:status で全て up か確認してください
sk48801728

2020/06/02 12:22

shcema.rb を確認したところtextカラムがあることは確認しております。 ------------------------------------------------------------------------------------------------------------- ActiveRecord::Schema.define(version: 2020_05_14_091501) do create_table "comments", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t| t.integer "user_id" t.integer "tweet_id" t.datetime "created_at", null: false t.datetime "updated_at", null: false t.text "text" end --------------------------------------------------------------------------------------------------------------------------------- RAILS_ENV=production rails db:migrate:status 打ち込んだ結果 up 20200425060916 Create tweets up 20200509024944 Devise create users up 20200509033058 Add nickname to users up 20200511090932 Add user id to tweets up 20200511102007 Remove name from tweets up 20200513095338 Create comments up 20200514091501 Add text to comments 全てupを確認いたしました。 ------------------------------------------------------------------------------------------------------------------------------- https://qiita.com/niyomong/items/22fa60657ea8ee478647 textを再度追加したのは上記質問を確認して、20200514091501_add_text_to_comments.rbのchangeメソッド定義内の記述を一度消してローカルをコミット,pul後に、rails db:migrate RAILS_ENV=productionを打ち込むと、 == 20200514091501 AddTextToComments: migrating ================================ == 20200514091501 AddTextToComments: migrated (0.0000s) ======================= l と表示され通っているようなのでchangeメソッド内の記述を戻し再度コミット、pullを行った後、 rails db:migrate RAILS_ENV=productionを打ち込むと、以下表示となりました。 [ec2-user@ip-172-31-4-94 social-engineer-tree]$ rails db:migrate RAILS_ENV=production [ec2-user@ip-172-31-4-94 social-engineer-tree]$ bundle exec unicorn_rails -c config/unicorn.rb -E production -D
winterboum

2020/06/02 14:24

それが解決した後のもうひとつのエラーは Errno::ENOENT: No such file or directory @ rb_sysopen - config/unicorn.rb とあります。 config/unicorn.rb を作ってください
sk48801728

2020/06/02 23:15

下記config/unicorn.rbファイル作成済みの状態でErrno::ENOENT: No such file or directory @ rb_sysopen - config/unicorn.rbが出ておりました。 config/unicorn.rbソースコード app_path = File.expand_path('../../', __FILE__) worker_processes 1 working_directory app_path pid "#{app_path}/tmp/pids/unicorn.pid" #ポート番号を指定 listen 3000 #エラーのログを記録するファイルを指定 stderr_path "#{app_path}/log/unicorn.stderr.log" #通常のログを記録するファイルを指定 stdout_path "#{app_path}/log/unicorn.stdout.log" #Railsアプリケーションの応答を待つ上限時間を設定 timeout 60 preload_app true GC.respond_to?(:copy_on_write_friendly=) && GC.copy_on_write_friendly = true check_client_connection false run_once = true before_fork do |server, worker| defined?(ActiveRecord::Base) && ActiveRecord::Base.connection.disconnect! if run_once run_once = false # prevent from firing again end old_pid = "#{server.config[:pid]}.oldbin" if File.exist?(old_pid) && server.pid != old_pid begin sig = (worker.nr + 1) >= server.worker_processes ? :QUIT : :TTOU Process.kill(sig, File.read(old_pid).to_i) rescue Errno::ENOENT, Errno::ESRCH => e logger.error e end end end after_fork do |_server, _worker| defined?(ActiveRecord::Base) && ActiveRecord::Base.establish_connection end
sk48801728

2020/06/03 09:37

less log/unicorn.stderr.logを入力すると、 log/unicorn.stderr.log: そのようなファイルやディレクトリはありません と表示されるためそもそもunicorn.rbを読み込めてないように見受けられます。 route.rbが入っているconfigディレクトリに入れているので収納先は合っていると思います。 ポート番号も3000としていますがインスタンスのセキュリティグループの設定もポートを3000としているので解放先も合っていると思います。
sk48801728

2020/06/03 10:10

less log/unicorn.stderr.log入力時のエラーが以下です。 そのためbundle exec unicorn_rails -c config/unicorn.rb -E production -Dを実行しても起動できません。 I, [2020-06-03T09:39:37.248107 #18145] INFO -- : Refreshing Gem list I, [2020-06-03T09:39:39.093002 #18145] INFO -- : listening on addr=0.0.0.0:3000 fd=10 I, [2020-06-03T09:39:39.098552 #18145] INFO -- : master process ready I, [2020-06-03T09:39:39.105055 #18154] INFO -- : worker=0 ready bundler: failed to load command: unicorn_rails (/home/ec2-user/.rbenv/versions/2.5.1/bin/unicorn_rails) ArgumentError: Already running on PID:18145 (or pid=/var/www/social-engineer-tree/tmp/pids/unicorn.pid is stale) /home/ec2-user/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/unicorn-5.4.1/lib/unicorn/http_server.rb:205:in `pid=' /home/ec2-user/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/unicorn-5.4.1/lib/unicorn/http_server.rb:137:in `start' /home/ec2-user/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/unicorn-5.4.1/bin/unicorn_rails:209:in `<top (required)>' /home/ec2-user/.rbenv/versions/2.5.1/bin/unicorn_rails:23:in `load' /home/ec2-user/.rbenv/versions/2.5.1/bin/unicorn_rails:23:in `<top (required)>'
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問