-
Notifications
You must be signed in to change notification settings - Fork 228
Open
Description
I have several forms that submit to a Virtus object (through a controller). Some forms contain an extras attribute, others don't.
I currently can't distinguish whether extras has been set to an empty array (i.e. the user deselects all checkboxes on the form) or whether extras has never been submitted. In either case, extras will be an empty array.
I need this distinction because if the user deselects all extras I need to update them. On the other hand, if extras were not part of the form (i.e. not in the params), I shouldn't update them.
class UpdateForm
include Virtus.model(nullify_blank: true)
include ActiveModel::Model
attribute :extras, Array
attribute :booking_time, Time
def save
updatable_attributes = self.attributes.delete_if { |key, value| value.blank? }
some_object.update(updatable_attributes)
end
endHow can I make Virtus to give me a nil on extras if I call it like this:
UpdateForm.new(booking_time: Time.current)Or is there a better pattern to do this?
Metadata
Metadata
Assignees
Labels
No labels