その他

ruby on railsでsave時にrollbackする原因がわからないとき。

普通にsaveすると以下のようになぜRollbackしているか分からない。
(lessonは任意のオブジェクト)

2.4.1 :019 > lesson.save
   (0.2ms)  BEGIN
  Lesson Exists (0.3ms)  SELECT  1 AS one FROM `lessons` WHERE `lessons`.`major_no` = 500 AND `lessons`.`course_id` = 11 LIMIT 1
   (0.3ms)  ROLLBACK
 => false 
2.4.1 :020 > 

だけどsave!として、!をつけるとエラー内容が表示される。
今回の場合は、titleカラムが空白なのでエラーになっていた。

2.4.1 :022 >   lesson.save!
   (0.2ms)  BEGIN
  Lesson Exists (0.5ms)  SELECT  1 AS one FROM `lessons` WHERE `lessons`.`major_no` = 500 AND `lessons`.`course_id` = 11 LIMIT 1
   (0.1ms)  ROLLBACK
ActiveRecord::RecordInvalid: Validation failed: Content can't be blank, Title can't be blank
        from /usr/local/rvm/gems/ruby-2.4.1/gems/activerecord-5.0.7/lib/active_record/validations.rb:78:in `raise_validation_error'
        from /usr/local/rvm/gems/ruby-2.4.1/gems/activerecord-5.0.7/lib/active_record/validations.rb:50:in `save!'
        from /usr/local/rvm/gems/ruby-2.4.1/gems/activerecord-5.0.7/lib/active_record/attribute_methods/dirty.rb:30:in `save!'
        from /usr/local/rvm/gems/ruby-2.4.1/gems/activerecord-5.0.7/lib/active_record/transactions.rb:324:in `block in save!'
        from /usr/local/rvm/gems/ruby-2.4.1/gems/activerecord-5.0.7/lib/active_record/transactions.rb:395:in `block in with_transaction_returning_status'
        from /usr/local/rvm/gems/ruby-2.4.1/gems/activerecord-5.0.7/lib/active_record/connection_adapters/abstract/database_statements.rb:232:in `block in transaction'
        from /usr/local/rvm/gems/ruby-2.4.1/gems/activerecord-5.0.7/lib/active_record/connection_adapters/abstract/transaction.rb:189:in `within_new_transaction'
        from /usr/local/rvm/gems/ruby-2.4.1/gems/activerecord-5.0.7/lib/active_record/connection_adapters/abstract/database_statements.rb:232:in `transaction'
        from /usr/local/rvm/gems/ruby-2.4.1/gems/activerecord-5.0.7/lib/active_record/transactions.rb:211:in `transaction'
        from /usr/local/rvm/gems/ruby-2.4.1/gems/activerecord-5.0.7/lib/active_record/transactions.rb:392:in `with_transaction_returning_status'
        from /usr/local/rvm/gems/ruby-2.4.1/gems/activerecord-5.0.7/lib/active_record/transactions.rb:324:in `save!'
        from /usr/local/rvm/gems/ruby-2.4.1/gems/activerecord-5.0.7/lib/active_record/suppressor.rb:45:in `save!'
        from (irb):22
        from /usr/local/rvm/gems/ruby-2.4.1/gems/railties-5.0.7/lib/rails/commands/console.rb:65:in `start'
        from /usr/local/rvm/gems/ruby-2.4.1/gems/railties-5.0.7/lib/rails/commands/console_helper.rb:9:in `start'
        from /usr/local/rvm/gems/ruby-2.4.1/gems/railties-5.0.7/lib/rails/commands/commands_tasks.rb:78:in `console'
        from /usr/local/rvm/gems/ruby-2.4.1/gems/railties-5.0.7/lib/rails/commands/commands_tasks.rb:49:in `run_command!'
        from /usr/local/rvm/gems/ruby-2.4.1/gems/railties-5.0.7/lib/rails/commands.rb:18:in `'
        from /usr/local/rvm/gems/ruby-2.4.1/gems/activesupport-5.0.7/lib/active_support/dependencies.rb:293:in `require'
        from /usr/local/rvm/gems/ruby-2.4.1/gems/activesupport-5.0.7/lib/active_support/dependencies.rb:293:in `block in require'
        from /usr/local/rvm/gems/ruby-2.4.1/gems/activesupport-5.0.7/lib/active_support/dependencies.rb:259:in `load_dependency'
        from /usr/local/rvm/gems/ruby-2.4.1/gems/activesupport-5.0.7/lib/active_support/dependencies.rb:293:in `require'
        from /home/ec2-user/environment/koooza/bin/rails:9:in `'
        from /usr/local/rvm/gems/ruby-2.4.1/gems/activesupport-5.0.7/lib/active_support/dependencies.rb:287:in `load'
        from /usr/local/rvm/gems/ruby-2.4.1/gems/activesupport-5.0.7/lib/active_support/dependencies.rb:287:in `block in load'
        from /usr/local/rvm/gems/ruby-2.4.1/gems/activesupport-5.0.7/lib/active_support/dependencies.rb:259:in `load_dependency'
        from /usr/local/rvm/gems/ruby-2.4.1/gems/activesupport-5.0.7/lib/active_support/dependencies.rb:287:in `load'
        from /usr/local/rvm/rubies/ruby-2.4.1/lib/ruby/site_ruby/2.4.0/rubygems/core_ext/kernel_require.rb:55:in `require'
        from /usr/local/rvm/rubies/ruby-2.4.1/lib/ruby/site_ruby/2.4.0/rubygems/core_ext/kernel_require.rb:55:in `require'
        from -e:1:in `
' 2.4.1 :023 >

save時にrollbackする際は、試してみてもよいかも。

-その他