# How to update nested Records?

{% hint style="warning" %}
**Not best practice:** Create an [opaque type](/elm-cookbook/recipes-1/making-impossible-states-impossible/restrict-records-using-opaque-types.md) instead.
{% endhint %}

{% tabs %}
{% tab title="Problem" %}

```
type alias Movie = 
    { title : String
    , rating : Int
    }
  
type alias Model = 
    { currentMovie : Movie
    }

rate : Int -> Model -> Model
rate newRating model =
    { model 
    | currentMovie = 
        --{ model.currentMovie | rating = newRating }
        Debug.todo "the line above does not compile"
    }
```

{% endtab %}

{% tab title="Solution" %}

```
type alias Movie = 
    { title : String
    , rating : Int
    }
  
type alias Model = 
    { currentMovie : Movie
    }

rate : Int -> Model -> Model
rate newRating ({currentMovie} as model) =
    { model 
    | currentMovie = 
        {currentMovie | rating = newRating }
    }
```

{% endtab %}

{% tab title="Alternative Solution" %}

```
type alias Movie = 
    { title : String
    , rating : Int
    }
  
type alias Model = 
    { currentMovie : Movie
    }

rate : Int -> Model -> Model
rate rating ({currentMovie} as model) =
    model
    |> updateMovie (addRating rating)

updateMovie : (Movie -> Movie) -> Model -> Model
updateMovie fun ({currentMovie} as model) =
    { model | currentMovie = fun currentMovie}
   
addRating : Int -> Movie -> Movie
addRating rating movie =
    { movie | rating = rating}
```

{% hint style="success" %}
**This is the a better solution**
{% endhint %}
{% endtab %}
{% endtabs %}

## Question

How can I update a nested record field?

## Answer

First get the nested record, then update it:

```
let
    currentMovie = model.currentMovie
in
{ currentMovie | rating = newRating}
```

{% hint style="info" %}
For function parameters we can use the keyword `as` to bind fields of a record to a variable with the same name:

```
rate newRating ({currentMovie} as model) =
    ...
```

{% endhint %}

## Further reading

* 📄**Article:** [Updating nested records in Elm](https://medium.com/elm-shorts/updating-nested-records-in-elm-15d162e80480) by Wouter In t Velt


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://orasund.gitbook.io/elm-cookbook/frequently-asked-questions-1/how-to-update-nested-records.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
