Is the Rails 4 Model Valid, But Won't Save?

I have the following models:

notifications

class Notification < ActiveRecord::Base
  belongs_to :notification_path
  before_save :set_default

  def set_default
      self.resolved = false unless self.resolved
  end
end

notification_path

class NotificationPath < ActiveRecord::Base
  has_many :notifications
end

Then this code:

notification = Notification.new ({"area" => "test", "severity" => "10", "message" => "Test", "notification_path_id" => 3})

 <Notification id: nil, area: "test", severity:
 10, message: "Test", created_at: nil, updated_at: nil,
 notification_path_id: 3, resolved: nil>

notification.valid?

     

True

     

notification.errors.full_messages

     

[]

So, as you can see - the notification is valid and has no errors. The notification model has zero validations. But then when I save the model:

notification.save

     

falsely

It will not save. What causes this? It may be worth noting that it set_defaultworks successfully, and although the model does not save the attribute resolved, when trying to save the value is set to false.

EDIT

, IRB:

  

notification.save! ActiveRecord:: RecordNotSaved: ActiveRecord:: RecordNotSaved from     /Users/Jonathan/.rvm/gems/ruby-2.1.1@steel_notify/gems/activerecord-4.1.0.rc1/lib/active_record/persistence.rb:125:in      save!' from /Users/Jonathan/.rvm/gems/ruby-2.1.1@steel_notify/gems/activerecord-4.1.0.rc1/lib/active_record/validations.rb:57:in !     /Users/Jonathan/.rvm/gems/ruby-2.1.1@steel_notify/gems/activerecord-4.1.0.rc1/lib/active_record/attribute_methods/dirty.rb:29:in     save!' from /Users/Jonathan/.rvm/gems/ruby-2.1.1@steel_notify/gems/activerecord-4.1.0.rc1/lib/active_record/transactions.rb:273:in ! "     /Users/Jonathan/.rvm/gems/ruby-2.1.1@steel_notify/gems/activerecord-4.1.0.rc1/lib/active_record/transactions.rb:329:in     block in with_transaction_returning_status' from /Users/Jonathan/.rvm/gems/ruby-2.1.1@steel_notify/gems/activerecord-4.1.0.rc1/lib/active_record/connection_adapters/abstract/database_statements.rb:211:in '     /Users/Jonathan/.rvm/gems/ruby-2.1.1@steel_notify/gems/activerecord-4.1.0.rc1/lib/active_record/connection_adapters/abstract/database_statements.rb:219:in     within_new_transaction' from /Users/Jonathan/.rvm/gems/ruby-2.1.1@steel_notify/gems/activerecord-4.1.0.rc1/lib/active_record/connection_adapters/abstract/database_statements.rb:211:in "     /Users/Jonathan/.rvm/gems/ruby-2.1.1@steel_notify/gems/activerecord-4.1.0.rc1/lib/active_record/transactions.rb:208:in     transaction' from /Users/Jonathan/.rvm/gems/ruby-2.1.1@steel_notify/gems/activerecord-4.1.0.rc1/lib/active_record/transactions.rb:326:in with_transaction_returning_status '     /Users/Jonathan/.rvm/gems/ruby-2.1.1@steel_notify/gems/activerecord-4.1.0.rc1/lib/active_record/transactions.rb:273:in     save!' from (irb):23 from /Users/Jonathan/.rvm/gems/ruby-2.1.1@steel_notify/gems/railties-4.1.0.rc1/lib/rails/commands/console.rb:90:in '     /Users/Jonathan/.rvm/gems/ruby-2.1.1@steel_notify/gems/railties-4.1.0.rc1/lib/rails/commands/console.rb:9:in     start' from /Users/Jonathan/.rvm/gems/ruby-2.1.1@steel_notify/gems/railties-4.1.0.rc1/lib/rails/commands/commands_tasks.rb:69:in '     /Users/Jonathan/.rvm/gems/ruby-2.1.1@steel_notify/gems/railties-4.1.0.rc1/lib/rails/commands/commands_tasks.rb:40:in     run_command!' from /Users/Jonathan/.rvm/gems/ruby-2.1.1@steel_notify/gems/railties-4.1.0.rc1/lib/rails/commands.rb:17:in '     /Users/Jonathan/.rvm/gems/ruby-2.1.1@steel_notify/gems/activesupport-4.1.0.rc1/lib/active_support/dependencies.rb:247:in     require' from /Users/Jonathan/.rvm/gems/ruby-2.1.1@steel_notify/gems/activesupport-4.1.0.rc1/lib/active_support/dependencies.rb:247:in '     /Users/Jonathan/.rvm/gems/ruby-2.1.1@steel_notify/gems/activesupport-4.1.0.rc1/lib/active_support/dependencies.rb:232:in     load_dependency' from /Users/Jonathan/.rvm/gems/ruby-2.1.1@steel_notify/gems/activesupport-4.1.0.rc1/lib/active_support/dependencies.rb:247:in require 'from/Users/Jonathan/steel_notify/bin/rails: 8: in <top (required)>' from /Users/Jonathan/.rvm/gems/ruby-2.1.1@steel_notify/gems/activesupport-4.1.0.rc1/lib/active_support/dependencies.rb:241:in load' from     /Users/Jonathan/.rvm/gems/ruby-2.1.1@steel_notify/gems/activesupport-4.1.0.rc1/lib/active_support/dependencies.rb:241:in     block in load' from /Users/Jonathan/.rvm/gems/ruby-2.1.1@steel_notify/gems/activesupport-4.1.0.rc1/lib/active_support/dependencies.rb:232:in load_dependency '     /Users/Jonathan/.rvm/gems/ruby-2.1.1@steel_notify/gems/activesupport-4.1.0.rc1/lib/active_support/dependencies.rb:241:in     load' from /Users/Jonathan/.rvm/rubies/ruby-2.1.1/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in require 'from     /Users/Jonathan/.rvm/rubies/ruby-2.1.1/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in     require' from -e:1:in

  
+4
1

Ruby . self.resolved false. false before_ *, . , false .

+10

Source: https://habr.com/ru/post/1535464/


All Articles