-
Notifications
You must be signed in to change notification settings - Fork 64
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Updating a record fails if a column has original value of false
#457
Comments
false
false
Thanks for the report. I'll take a look at this. |
This change seems to fix it: # ...
private def _{{ attribute[:name] }}
record_value = @record.try(&.{{ attribute[:name] }})
value = record_value.nil? ? default_value_for_{{ attribute[:name] }} : record_value
@_{{ attribute[:name] }} ||= Avram::Attribute({{ attribute[:type] }}?).new(
name: :{{ attribute[:name].id }},
param: permitted_params["{{ attribute[:name] }}"]?,
value: value,
param_key: self.class.param_key)
end
#... |
Ok, I'm taking a look in to this, but I'm having some trouble understanding what the issue is. Using Avram master, I wrote these two specs: model = ModelWithDefaultValuesBox.create
params = Avram::Params.new({"greeting" => "Hi"})
OverrideDefaults.update(model, params) do |_operation, record|
record.should_not eq nil
r = record.not_nil!
r.greeting.should eq "Hi"
r.admin.should eq false
end
user = UserBox.create &.available_for_hire(true)
params = Avram::Params.new({"available_for_hire" => "false"})
SaveUser.update(user, params) do |_operation, record|
record.should_not eq nil
r = record.not_nil!
r.available_for_hire.should eq false
end and both of these specs pass. In this case, the Can you give a little more context as to what |
class UserOptions < BaseModel
table :user_options do
belongs_to user : User
column login_notify : Bool
column password_notify : Bool
end
end The two columns have no default values. In class SaveUserOptions < UserOptions::SaveOperation
permit_columns :login_notify, :password_notify
before_save do
validate_required login_notify, password_notify
validate_required user_id, message: "does not exist"
end
end If SaveUserOptions.update(
user.options!,
# NOTICE we are not passing `password_notify` here, even though it is required.
# I don't know if this was intended, but that is how Avram works currently.
# It seems Avram merges in the columns from the record (`user.options!`)
# in update operations.
Avram::Params.new({"login_notify" => "false"})
) do |operation, updated_user_options|
updated_user_options.login_notify.should be_false # <= PASSES
updated_user_options.password_notify.should be_true # <= PASSES
end However, If Avram requires the Hope this helps? |
avram/src/avram/save_operation.cr Line 126 in fedd1be
I believe this bug happened in the line of code above, when it was changed from The added |
This seemed to have failed as a result of a regression in Lucky v0.24.0. See luckyframework/avram#457
Ah, ok. Yeah, that helps. I have a failing spec now which seems to be related to the |
* Fixing issue where a required validation on a bool field would fail if the value was false. Fixes #457 * applying spec cleanup changes * Removing this box since it's not actually needed for anything
The following fails, with an error:
The error is
{:password_notify => ["is required"]}
Seems the operation fails because
password_notify
has original value offalse
. If you explicitly passpassword_notify: false
to the operation, it works OK. Otherwise, ifpassword_notify
has original value oftrue
, it works OK.This seems to be a regression in #424. My guess is this line:
avram/src/avram/save_operation.cr
Line 126 in fedd1be
The text was updated successfully, but these errors were encountered: