Pantrypoints Pantrypoints

Ecto Changeset

April 15, 2022 1 minutes  • 180 words
Table of contents

Changsets allow manipulation of the data to match Schema so that it can be passed to the Repo

  • Cast: filters the parameters with the attributes to be accepted into the changeset
  • Validate: validates those filtered attributes. This includes constraints which are set on the database level

Schema’d Changeset

Schemas are a useful shortcut used in querying so you don’t have to specify all the attributes

Defining

def changeset(schemaname, attrs) do
  schemaname
  |> cast(attrs, [:key1, :key2,..])
  |> validate_required([:key1])
  |> validate_inclusion(:name, ["John", "Jack"])
  |> validate_exclusion(:name, ["Jub Jub"])
  |> validate_length(:country, is: 2)
  |> validate_length(:password, min: 8, max: 32)
end

Calling

attrib_name = get_field(changeset, :attrib_name)

Schemaless Changeset

Defining

model = %{key1: :string, key2: string} 
attributes = %{key1: "value", key2: "value"}    
changeset = {%{}, model}
  |> cast(attributes, Map.keys(model))
  |> validate_required([:key1])
end

Schema Associations

sets relationships between tables

  • has_many
  • has_one
  • belongs_to
  • many_to_many

Virtual Attributes

Disposable attributes that are not saved to the database. For example, an office address that is used to get a latitude and longitude to store in the database, without storing the office address

Embedded Schema

A schema that doesn’t use a datbase

Follow Us! →

We're creating a new Economic System from a new Economic Science! Please support us by leaving your email or leaving a comment above.