tjinjin's blog

インフラ要素多めの個人メモ

Railstutorial学習記録4

ずっと放置していたのですが、GWで時間取れそうというのと仕事を進めていく上でアプリ側の知識があったほうが、インフラの仕事も円滑に進むと思ったので、再開します。

以前やった部分忘れてますが3章からやります。

error

rails genarate rspec:install

$ rails generate rspec:install
Users/cross/work/rails_projects/sample_app/config/environments/development.rb:1:in `<top (required)>': undefined method `configure' for #<SampleApp::Application:0x007fefad130fe8> (NoMethodError)
        from /Users/cross/work/rails_projects/sample_app/work/gems/activesupport-4.0.5/lib/active_support/dependencies.rb:229:in `require'
        from /Users/cross/work/rails_projects/sample_app/work/gems/activesupport-4.0.5/lib/active_support/dependencies.rb:229:in `block in require'
        from /Users/cross/work/rails_projects/sample_app/work/gems/activesupport-4.0.5/lib/active_support/dependencies.rb:214:in `load_dependency'
        from /Users/cross/work/rails_projects/sample_app/work/gems/activesupport-4.0.5/lib/active_support/dependencies.rb:229:in `require'
        from /Users/cross/work/rails_projects/sample_app/work/gems/railties-4.0.5/lib/rails/engine.rb:591:in `block (2 levels) in <class:Engine>'
        from /Users/cross/work/rails_projects/sample_app/work/gems/railties-4.0.5/lib/rails/engine.rb:590:in `each'
        from /Users/cross/work/rails_projects/sample_app/work/gems/railties-4.0.5/lib/rails/engine.rb:590:in `block in <class:Engine>'
        from /Users/cross/work/rails_projects/sample_app/work/gems/railties-4.0.5/lib/rails/initializable.rb:30:in `instance_exec'
        from /Users/cross/work/rails_projects/sample_app/work/gems/railties-4.0.5/lib/rails/initializable.rb:30:in `run'
        from /Users/cross/work/rails_projects/sample_app/work/gems/railties-4.0.5/lib/rails/initializable.rb:55:in `block in run_initializers'
        from /Users/cross/.rbenv/versions/2.0.0-p353/lib/ruby/2.0.0/tsort.rb:150:in `block in tsort_each'
        from /Users/cross/.rbenv/versions/2.0.0-p353/lib/ruby/2.0.0/tsort.rb:183:in `block (2 levels) in each_strongly_connected_component'
        from /Users/cross/.rbenv/versions/2.0.0-p353/lib/ruby/2.0.0/tsort.rb:210:in `block (2 levels) in each_strongly_connected_component_from'
        from /Users/cross/.rbenv/versions/2.0.0-p353/lib/ruby/2.0.0/tsort.rb:219:in `each_strongly_connected_component_from'
        from /Users/cross/.rbenv/versions/2.0.0-p353/lib/ruby/2.0.0/tsort.rb:209:in `block in each_strongly_connected_component_from'
        from /Users/cross/work/rails_projects/sample_app/work/gems/railties-4.0.5/lib/rails/initializable.rb:44:in `each'
        from /Users/cross/work/rails_projects/sample_app/work/gems/railties-4.0.5/lib/rails/initializable.rb:44:in `tsort_each_child'
        from /Users/cross/.rbenv/versions/2.0.0-p353/lib/ruby/2.0.0/tsort.rb:203:in `each_strongly_connected_component_from'
        from /Users/cross/.rbenv/versions/2.0.0-p353/lib/ruby/2.0.0/tsort.rb:182:in `block in each_strongly_connected_component'
        from /Users/cross/.rbenv/versions/2.0.0-p353/lib/ruby/2.0.0/tsort.rb:180:in `each'
        from /Users/cross/.rbenv/versions/2.0.0-p353/lib/ruby/2.0.0/tsort.rb:180:in `each_strongly_connected_component'
        from /Users/cross/.rbenv/versions/2.0.0-p353/lib/ruby/2.0.0/tsort.rb:148:in `tsort_each'
        from /Users/cross/work/rails_projects/sample_app/work/gems/railties-4.0.5/lib/rails/initializable.rb:54:in `run_initializers'
        from /Users/cross/work/rails_projects/sample_app/work/gems/railties-4.0.5/lib/rails/application.rb:215:in `initialize!'
        from /Users/cross/work/rails_projects/sample_app/config/environment.rb:5:in `<top (required)>'
        from /Users/cross/work/rails_projects/sample_app/work/gems/railties-4.0.5/lib/rails/application.rb:189:in `require'
        from /Users/cross/work/rails_projects/sample_app/work/gems/railties-4.0.5/lib/rails/application.rb:189:in `require_environment!'
        from /Users/cross/work/rails_projects/sample_app/work/gems/railties-4.0.5/lib/rails/commands.rb:44:in `<top (required)>'
        from bin/rails:4:in `require'
        from bin/rails:4:in `<main>'
cross@~/work/rails_projects/sample_app   [2.0.0-p353] [15-05-04 0:38]`

configureというメソッドが見つからないらしい。config/environment/development.rbを確認すると、

Rails.application.configure do

となっていました。[undefined method `configure' エラーの対処法 - ベスパライフ] (http://takeg.hatenadiary.jp/entry/2014/10/24/183716)を見て、

SampleApp::Application.configure do

に変更して無事解決。

herokuへのデプロイ時にエラーが発生

$ heroku run rake db:migrate
Running `rake db:migrate` attached to terminal... up, run.3332
rake aborted!
NoMethodError: undefined method `dump_schema_after_migration=' for ActiveRecord::Base:Class

[ruby on rails - Migration error on heroku deploy - Stack Overflow] (http://stackoverflow.com/questions/25564012/migration-error-on-heroku-deploy)を参考に設定を変更。bundle listでrailsのバージョン確認すると確かに4系になってた。

$ mvim config/environments/production.rb
  # Do not dump schema after migrations.
#  config.active_record.dump_schema_after_migration = false
end

設定変更後、heroku にpush した後にmigrateしたらうまくいきました。

spork導入時にエラーが発生

$ bundle install
Fetching gem metadata from https://rubygems.org/.........
Fetching version metadata from https://rubygems.org/..
You have requested:
  childprocess = 0.3.6

The bundle currently has childprocess locked at 0.5.6.
Try running `bundle update childprocess`

childprocessというgemは0.3.6は使えないということみたい。仕方ないので、バージョンを0.5.6を指定してbundle installしたらうまくいきました。

sporkを使っているときに--drbオプションをつけるとエラーになった。

Spork導入してRSpec動かそうとした時にActiveRecord::ConnectionNotEstablishedでハマった - Qiita同じような現象の記事があったので、それを参考に修正したつもりなんですが、変わらず。本筋ではないのでオプションないで動かすことにして、解消法わかったら追記します。

sqlite -> postgresqlに変更したらエラー

ここは諦めました。たぶんpostgresql-develとかを事前にインストールしてあげたりしないといけないのかな。

感想

  • 便利そうなgemを学んだ!
  • rubygems-bundler 一々bundle execを叩かずにコマンドを実行できる。個人的にはbundle execはaliasでbeにしていたので、そこまで気にはならないですがね。タイプ減らせるので便利ですね。
  • guard specファイルを監視させて、テストの変更を検知したら自動でテストを実行してくれる。TDDやる上では便利そうですね。
  • spork テストの高速化。環境情報を毎度読み込むのは時間がかかるので、事前に読み込んでキャッシュのような感じで使う。そこまで体感では早くなった印象はなかったですね。というかそもそもうまく使えてない気がします。

次は4章