Skip to content

Commit 7f6992a

Browse files
committed
Fix finding primary key when a query is passed
1 parent 5e3fbe8 commit 7f6992a

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

lib/plasm.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,7 @@ defmodule Plasm do
453453
key
454454
end
455455

456-
defp model(%Ecto.Query{from: {_table_name, model_or_query}}) do
456+
defp model(%Ecto.Query{from: %Ecto.Query.FromExpr{source: {_, model_or_query}}}) do
457457
model(model_or_query)
458458
end
459459
defp model(model), do: model

test/plasm/find_test.exs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,17 @@ defmodule Plasm.FindTest do
3333
# Assert
3434
assert user == user1
3535
end
36+
37+
test ".find with an integer and a prior query" do
38+
# Arrange
39+
user1 = insert(:user, age: 18)
40+
insert(:user, age: 18)
41+
insert(:user, age: 81)
42+
43+
# Act
44+
user = User |> User.for_age(18) |> Plasm.find(user1.id) |> Repo.one
45+
46+
# Assert
47+
assert user == user1
48+
end
3649
end

test/support/models.ex

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
defmodule Plasm.User do
22
use Ecto.Schema
3+
import Ecto.Query
34

45
schema "users" do
56
field :name, :string
@@ -8,4 +9,9 @@ defmodule Plasm.User do
89

910
timestamps(type: :utc_datetime)
1011
end
12+
13+
def for_age(query, age) do
14+
from u in query,
15+
where: u.age == ^age
16+
end
1117
end

0 commit comments

Comments
 (0)