Skip to content

Instantly share code, notes, and snippets.

@mazz
Created May 21, 2019 14:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mazz/b7380242faf8defba6e5a5101fcd8491 to your computer and use it in GitHub Desktop.
Save mazz/b7380242faf8defba6e5a5101fcd8491 to your computer and use it in GitHub Desktop.
iex(2)> import Ecto.Query
Ecto.Query
iex(3)> Video |> select([v], v.id)
#Ecto.Query<from v0 in DB.Schema.Video, select: v0.id>
iex(4)> Video |> select([v], v.id) |> DB.Repo.all()
[debug] QUERY OK source="videos" db=1.9ms decode=2.6ms queue=6.5ms
SELECT v0."id" FROM "videos" AS v0 []
[1]
iex(5)> Video |> select([v], v.id) |> DB.Repo.all() |> Enum.map(&Video.changeset_generate_hash_id/1)
[debug] QUERY OK source="videos" db=0.3ms
SELECT v0."id" FROM "videos" AS v0 []
** (FunctionClauseError) no function clause matching in DB.Schema.Video.changeset_generate_hash_id/1
The following arguments were given to DB.Schema.Video.changeset_generate_hash_id/1:
# 1
1
Attempted function clauses (showing 1 out of 1):
def changeset_generate_hash_id(video = %{id: id})
(db) lib/db_schema/video.ex:157: DB.Schema.Video.changeset_generate_hash_id/1
(elixir) lib/enum.ex:1327: Enum."-map/2-lists^map/1-0-"/2
iex(5)> DB.Schema.Video.changeset_generate_hash_id(%DB.Schema.Video{id: 42, hash_id: nil})
#Ecto.Changeset<
action: nil,
changes: %{hash_id: "GMAA"},
errors: [],
data: #DB.Schema.Video<>,
valid?: true
>
iex(6)>
def changeset_generate_hash_id(video = %{id: id}) do
change(video, hash_id: VideoHashId.encode(id))
end
@mazz
Copy link
Author

mazz commented May 21, 2019

Video |> select([v], map(v, [:id])) |> DB.Repo.all()

[debug] QUERY OK source="videos" db=2.0ms queue=1.8ms
SELECT v0."id" FROM "videos" AS v0 []
[%{id: 1}]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment