Created
March 21, 2019 02:09
-
-
Save chouzar/a0c8934110859ddcd226dc6466547c51 to your computer and use it in GitHub Desktop.
Seed example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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