ActiveRecord :: RecordInvalid:验证失败:密钥不能为空

时间:2019-04-15 04:40:42

标签: ruby-on-rails ruby activerecord

更新到Rails 4.0后,出现此错误。

nodetool disablehandoff

这是方法:

ActiveRecord::RecordInvalid (Validation failed: Key can't be blank):
  app/models/users_setting.rb:25:in `update_value'
  app/controllers/management_reports/employee_onboarding_controller.rb:35:in `update_filter_values'
  app/controllers/management_reports/employee_onboarding_controller.rb:57:in `prepare_to_read_data'
  app/controllers/management_reports/employee_onboarding_controller.rb:11:in `index'

撬给我看这个:

def update_value options={}
  binding.pry
    self.update_attributes!({:value => options.inspect})
  end

不确定如何进行,请提供任何帮助。

1 个答案:

答案 0 :(得分:2)

请参阅选项本身是一个哈希,因此您只需要传递选项try,

self.update_attributes!(options)

其中状态 client_id 是模型属性。

由于可能将这两个字段的状态设置为true,因此出现Rails验证错误消息时,键不能为空,值不能为空。

如果您想跳过验证,可以执行以下操作,以便在更新rails时不会大声疾呼键和值。

def update_value options={}
  self.status = options[:status]
  self.client_id = options[:client_id]
  self.save(validate: false)
end