我有以下模型 -
class Project < ActiveRecord::Base
enum commitment: {
more_than_30hrs_week: 'More than 30hrs/week',
less_than_30hrs_week: 'Less than 30hrs/week',
dont_know: 'Dont know'
}
enum status: [:open, :closed, :archived]
validates :commitment, :status :presence => true
end
当我尝试更新项目对象时,如下所示 -
@project.update!(project_params)
其中 -
@project = #<Project:0x007fde227059d8
id: 5,
commitment: "less than 30hrs/week",
status: 2 >
project_params = {"status"=>"archived"}
它引发了我的错误 -
ActiveRecord::RecordInvalid: Validation failed: Commitment can't be blank
我无法理解为什么它会抛出此错误,因为承诺值已经存在且我没有传递要更新的属性。
执行以下工作,但我不希望这样,因为我想更新多个属性 -
@project.update_attribute(:status, 'archived')
有人可以解释一下,我怎样才能做到这一点?