diff --git a/spec/granite/transactions/update_spec.cr b/spec/granite/transactions/update_spec.cr index d0f3e348..f76cd01b 100644 --- a/spec/granite/transactions/update_spec.cr +++ b/spec/granite/transactions/update_spec.cr @@ -11,6 +11,16 @@ describe "#update" do Parent.find!(parent.id).name.should eq "Other parent" end + it "allows setting a value to nil" do + model = Teacher.create!(name: "New Parent") + + model.update(name: nil) + + model.name.should be_nil + + Teacher.find!(model.id).name.should be_nil + end + it "does not update an invalid object" do parent = Parent.new(name: "New Parent") parent.save! diff --git a/src/granite/columns.cr b/src/granite/columns.cr index 77357869..464b1043 100644 --- a/src/granite/columns.cr +++ b/src/granite/columns.cr @@ -118,7 +118,7 @@ module Granite::Columns def set_attributes(hash : Hash(String | Symbol, Type)) : self {% for column in @type.instance_vars.select { |ivar| (ann = ivar.annotation(Granite::Column)) && (!ann[:primary] || (ann[:primary] && ann[:auto] == false)) } %} - if hash.has_key?({{column.stringify}}) && !hash[{{column.stringify}}].nil? + if hash.has_key?({{column.stringify}}) begin val = Granite::Type.convert_type hash[{{column.stringify}}], {{column.type}} rescue ex : ArgumentError