From 4b0df53502da84da75804bf4d1fb983e5e6a2c39 Mon Sep 17 00:00:00 2001 From: Paul Bob <69730720+Paul-Bob@users.noreply.github.com> Date: Tue, 12 Mar 2024 17:41:10 +0200 Subject: [PATCH] update_using (#181) --- docs/3.0/field-options.md | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/docs/3.0/field-options.md b/docs/3.0/field-options.md index 61765d91..182ea9fd 100644 --- a/docs/3.0/field-options.md +++ b/docs/3.0/field-options.md @@ -120,6 +120,17 @@ You can also format using Rails helpers like `number_to_currency` (note that `vi field :price, as: :number, format_using: -> { view_context.number_to_currency(value) } ``` +## Parse value before update +When it's necessary to parse information before storing it in the database, the `update_using` option proves to be useful. Inside the block you can access the raw `value` from the form, and the returned value will be saved in the database. + +```ruby +field :metadata, + as: :code, + update_using: -> do + ActiveSupport::JSON.decode(value) + end +``` + ## Sortable fields One of the most common operations with database records is sorting the records by one of your fields. For that, Avo makes it easy using the `sortable` option.