Rails - 将模型值设置为nil

时间:2009-12-21 04:47:05

标签: ruby-on-rails activerecord

当试图将模型中的一个列值设置为nil时,Rail的ActiveRecord是否有任何特殊规则?

freez方法中的完整性检查总是失败。

模型:项目

列:non_project_costs_value(可为空的十进制字段)

def froz?
    return !(non_project_costs_value.nil?)
end


def freez(val)
    raise 'Already frozen, cannot freeze ... the value is ' + non_project_costs_value.to_s if froz?
    non_project_costs_value = val
    save
    raise 'Sanity check.  After freezing the thing should be frozen!' if !froz?
end

1 个答案:

答案 0 :(得分:2)

你的freez方法有一个错误

non_project_costs_value = val # this is setting a local variable
                              # (in the method freez) to val

# change to:
self.non_project_costs_value = val # this will set the record's attribute
                                   # (the column) to val
                                   # It will be saved to the dbms once you save