Rails 4 model is valid, but won't save?
I have the following models:
notification
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 - notification is valid and has no errors. There are zero validations on the notification model. But then, when I save the model:
notification.save
false
 It won't save.  What is causing this?  It may be worth noting that set_default does run successfully, and even though the model isn't saving the resolved attribute is being set to false when it tries to save.  
EDIT
Full error trace below, when following the above in 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  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 save!'  from /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  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 block in save!'  from /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 block in 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: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 transaction' from /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_statu  s' from /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  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 start' from /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 console' from /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  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 ' 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/.rvm/gems/ruby-2.1.1@steel_notify/gems/activesupport-4.1.0.rc1/lib/active_support/dependencies.rb:247:in block in require' 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' 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  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' 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/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 '  
 In Ruby, assignments return the value being assigned.  In your case you are setting self.resolved to false .  When you return exactly false from a before_* callback, it will cancel the save operation.  You need to return something that is not false after you make your assignment.  
上一篇: 在宝石的耙子任务中需要一颗宝石
下一篇: Rails 4模型是有效的,但不会保存?
