concept record in category elixir

appears as: records, record, records, record
Phoenix in Action

This is an excerpt from Manning's book Phoenix in Action.

Listing 8.15. Updating a record using your changeset
iex(1)> item = Auction.list_items() |> Enum.at(0)
iex(2)> item |>                                                #1
...(2)> Auction.Item.changeset(
...(2)> %{title: nil, ends_at: DateTime.from_naive!(~N[1980-01-01 00:00:00],
     "Etc/UTC")}) |>
...(2)> Auction.Repo.update()
{:error,                                                       #2
 #Ecto.Changeset<
   action: :update,
   changes: %{ends_at: #DateTime<1980-01-01 00:00:00Z>},
   errors: [
     ends_at: {"ends_at cannot be in the past", []},
     title: {"can't be blank", [validation: :required]}
   ],
   data: #Auction.Item<>,
   valid?: false
 >}

iex(3)> item |>
...(3)> Auction.Item.changeset(
...(3)> %{title: "My updated item", ends_at:
     DateTime.from_naive!(~N[2020-01-01 00:00:00], "Etc/UTC")}) |>
...(3)> Auction.Repo.update()
22:29:32.383 [debug] QUERY OK db=10.5ms
# ... Generated SQL removed
{:ok,
 %Auction.Item{
   __meta__: #Ecto.Schema.Metadata<:loaded, "items">,
   description: "Computer games and thermonuclear war",
   ends_at: #DateTime<2020-01-01 00:00:00Z>,
   id: 1,
   inserted_at: ~N[2018-12-12 02:54:15],
   title: "My updated item",
   updated_at: ~N[2018-12-12 03:39:55]
 }}

As I mentioned early in this chapter, the Ecto.Repo module has update/2 and update!/2 functions that update the records in the database. The former returns a tuple containing its status (:ok or :error) and the changeset it attempted to use (on error) or the database record after it was updated (on success). The latter just contains the updated record. If there was an error in the Ecto.Repo.update!/2 call, it would raise an error instead of just letting you know about it in the return tuple.

Let’s do a bit of exploring in IEx. The first thing you’ll try is to update a record with bad data. Then, you’ll correct your errors and try updating the record with the corrected data. Follow along in your own IEx session if you like.

Listing 8.15. Updating a record using your changeset
iex(1)> item = Auction.list_items() |> Enum.at(0)
iex(2)> item |>                                                #1
...(2)> Auction.Item.changeset(
...(2)> %{title: nil, ends_at: DateTime.from_naive!(~N[1980-01-01 00:00:00],
     "Etc/UTC")}) |>
...(2)> Auction.Repo.update()
{:error,                                                       #2
 #Ecto.Changeset<
   action: :update,
   changes: %{ends_at: #DateTime<1980-01-01 00:00:00Z>},
   errors: [
     ends_at: {"ends_at cannot be in the past", []},
     title: {"can't be blank", [validation: :required]}
   ],
   data: #Auction.Item<>,
   valid?: false
 >}

iex(3)> item |>
...(3)> Auction.Item.changeset(
...(3)> %{title: "My updated item", ends_at:
     DateTime.from_naive!(~N[2020-01-01 00:00:00], "Etc/UTC")}) |>
...(3)> Auction.Repo.update()
22:29:32.383 [debug] QUERY OK db=10.5ms
# ... Generated SQL removed
{:ok,
 %Auction.Item{
   __meta__: #Ecto.Schema.Metadata<:loaded, "items">,
   description: "Computer games and thermonuclear war",
   ends_at: #DateTime<2020-01-01 00:00:00Z>,
   id: 1,
   inserted_at: ~N[2018-12-12 02:54:15],
   title: "My updated item",
   updated_at: ~N[2018-12-12 03:39:55]
 }}

You’ve now successfully updated a record in your database.

  • Records— Each record in a table has a key that uniquely identifies each row or record in the table. Typically, this is an integer ID, but it can be a number of other things (like a UUID).