我有以下型号:
通知
class Notification < ActiveRecord::Base
belongs_to :notification_path
before_save :set_default
def set_default
self.resolved = false unless self.resolved
end
end
通知路径
class NotificationPath < ActiveRecord::Base
has_many :notifications
end
然后这段代码:
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
[]
因此,如您所见 - 通知有效且没有错误。通知模型的验证为零。但是,当我保存模型时:
notification.save
false
它不会保存。这是什么原因造成的?值得注意的是 set_default
确实运行成功,即使模型没有保存 resolved
尝试保存时属性被设置为 false。
编辑
在 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/attribute_methods/dirty.rb:29:insave!' 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:inblock 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:inwithin_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:intransaction' 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' from /Users/Jonathan/.rvm/gems/ruby-2.1.1@steel_notify/gems/activerecord-4.1.0.rc1/lib/active_record/transactions.rb:273:insave!' 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:instart' 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:inrun_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:inrequire' 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:inload_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:inblock 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:inload' 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:inrequire' from -e:1:in
'
最佳答案
在 Ruby 中,赋值返回被赋值的值。在您的情况下,您将 self.resolved
设置为 false
。当您从 before_* 回调中准确返回 false
时,它将取消保存操作。完成分配后,您需要返回不是 false
的内容。
关于ruby-on-rails - Rails 4 模型有效,但不会保存?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22926614/