how to make a column value to null through model in ruby on rails? For example, I have a checkbox called 'include_on_epc' and the textbox called 'removal_reason'.
If the checkbox is checked, I want to set the value of the textbox should be NULL in database.
I tried the following, its not working.
class Emm::Rrr::Result < ActiveRecord::Base
before_save :no_removal_reason_when_including_on_epc
private
def no_removal_reason_when_including_on_epc
if include_on_epc == 1
self.removal_reason == nil
end
end
end
Two issues here.
As Jakob pointed out,
self.removal_reason == nilcomparesremoval_reasontonil, and you want to setremoval_reasontonil. Thereforeself.removal_reason = nilis definitely what you want here.If
include_on_epcis abooleancolumn, comparing to1is not going to work. You probably want a simpleif include_on_epcbecause its values are likelytrueorfalse, not1or0, and in Ruby1 != true.