Skip to content

Instantly share code, notes, and snippets.

@chouzar
Created March 21, 2019 02:09
Show Gist options
  • Select an option

  • Save chouzar/a0c8934110859ddcd226dc6466547c51 to your computer and use it in GitHub Desktop.

Select an option

Save chouzar/a0c8934110859ddcd226dc6466547c51 to your computer and use it in GitHub Desktop.
Seed example
alias Vento.Partners.SponsorType
alias Vento.Repo
# The NaiveDateTime is recognized by ecto for date related types
now =
NaiveDateTime.utc_now()
|> NaiveDateTime.truncate(:second)
# Define record structure with :inserted_at and :updated_at fields
records =
[
%{type: "Gold"},
%{type: "Platinum"}
]
|> Enum.map(&Map.put(&1, :inserted_at, now))
|> Enum.map(&Map.put(&1, :updated_at, now))
# Apply changesets to check if all records are valid
valid? =
records
|> Enum.map(&SponsorType.new/1) # Apply changeset
|> Enum.all?(fn x -> x.valid? end) # Check if changeset is valid
# Insert All
if valid? do
Repo.insert_all(SponsorType, records)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment