开发者

Rails - setting model values to nil

Does Rail's ActiveRecord have any special rules when attempting to set one of the column values in the model to nil?

The sanity check in the method freez always fails.

Model: Project

Column: non_project_costs_value (nullable decimal field)

def froz?
    return !(non_project_costs_valu开发者_运维技巧e.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


You have a bug in your freez method

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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜