Lui*_*igi 4 validation model ruby-on-rails
我有以下型号:
class Notification < ActiveRecord::Base
belongs_to :notification_path
before_save :set_default
def set_default
self.resolved = false unless self.resolved
end
end
Run Code Online (Sandbox Code Playgroud)
class NotificationPath < ActiveRecord::Base
has_many :notifications
end
Run Code Online (Sandbox Code Playgroud)
然后这段代码:
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>
Run Code Online (Sandbox Code Playgroud)
notification.valid?
真正
notification.errors.full_messages
[]
因此,正如您所看到的 - 通知有效且没有错误.通知模型上的验证为零.但是,当我保存模型时:
notification.save
假
它不会保存.是什么造成的?可能值得注意的是,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:insave !来自/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:inblock in save!' 来自/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:inblock in the 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:intransaction'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:inwith_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:instart from 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:inconsole'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'来自/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:inblock in require' 来自/Users/Jonathan/.rvm/gems/ruby-2.1.1@steel_notify/gems/activesupport-4.1.0.rc1/lib/active_support/dependencies.rb:232:in require'fromload_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/ 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:inload'fromblock 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/Users/Jonathan/.rvm/gems/ruby-2.1.1@steel_notify/gems/activesupport-4.1.0.rc1/lib/active_support/dependencies.rb:241: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 require'fromload' from /Users/Jonathan/.rvm/rubies/ruby-2.1.1/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in/ 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'
Log*_*man 11
在Ruby中,赋值返回分配的值.在你的情况要设置self.resolved到false.当您false从before_*回调中完全返回时,它将取消保存操作.你需要false在完成作业后返回一些不属于你的东西.
| 归档时间: |
|
| 查看次数: |
2040 次 |
| 最近记录: |