Daniel
Daniel

Reputation: 16226

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_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

Upvotes: 0

Views: 1769

Answers (1)

Larry K
Larry K

Reputation: 49104

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

Upvotes: 2

Related Questions