Reputation: 1594
I'm trying to create an ecto query that returns a result when both where queries are met. When I run the app, I get the error function nil.title/0 is undefined or private
. Is this because I don't have the query right? I want an AND on the two where.
def next_prod do
Post |> where(postStatus: "published") |> where(next_prod: "yes") |> Repo.one()
end
SQL query
You'll see there is a record that should match the ecto query.
iex(1)> Codsonline.Repo.query("SELECT * FROM posts")
[debug] QUERY OK db=3.0ms queue=0.1ms
SELECT * FROM posts []
{:ok,
%Postgrex.Result{columns: ["id", "title", "snippet", "body", "slug", "mainImg",
"thumbImg", "postStatus", "showStatus", "inserted_at", "updated_at",
"next_prod"], command: :select, connection_id: 13732, num_rows: 1,
rows: [[8, "Blackadder Goes Forth",
"“Blackadder Goes Forth ” is the fourth and final part of the Blackadder series . A theatrical Dramatisation inspired by the TV episodes. Follow the Wit and Sarcasm of Captain BlackAdder in Oscar Wilde language.",
"27th January – 03rd February 2018\r\nPlayhouse Theatre, Cheltenham\r\n\r\n \r\n\r\nEpisodes:\r\n‘Corporal Punishment‘ (Episode 2)\r\n‘Private Plane‘ (Episode 4)\r\n‘Goodbyeee‘ (Episode 6)\r\n\r\nDirected by Nick Tobias & Liz White\r\n\r\n \r\n\r\nCast List:\r\n\r\nCaptain Edmund Blackadder – Chris Hannant\r\nPrivate S Baldrick – Matt Wilson\r\nLt. Hon. George Colthurst St Barleigh – UNCAST\r\nGeneral Sir Anthony Cecil Hogmanay Melchett – Jason Blackburn\r\nCaptain Kevin Darling – Jack Homer\r\nSquadron Commander the Lord Flashheart – Michael Fay\r\nField Marshall Sir Douglas Haig – Will Browne\r\nBaron Von Richthoven – Robert Barton-Ancliffe\r\nBob ‘Bobbie’ Parkhurst – Sarah Bottomley\r\n\r\nEnsemble – Ben Wilson (Sergeant Jones), Oliver Chapman (Gerhardt), Michael Sheldrick (Perkins), Steve Scott, Matthew Morris",
"blackadder-goes-forth", ".", ".", "published", "Future",
{{2017, 11, 18}, {22, 36, 19, 408441}},
{{2017, 11, 18}, {23, 3, 42, 816258}}, "Yes"]]}}
Upvotes: 2
Views: 751
Reputation: 222388
That error is not from this line of code. Your code is fine and the two clauses will be AND'd by Ecto. The error means that you tried to call nil.title
somewhere.
iex(1)> nil.title
** (UndefinedFunctionError) function nil.title/0 is undefined or private
nil.title()
I'm guessing you called .title
on the return value of this function and you don't actually have a record that matches those two conditions in the database. You should use Repo.one!
if you want to ensure that a record exists. Or if you want to handle the case where the record doesn't exist, use if
:
if record = Thing.next_prod do
# record exists
else
# it doesn't
end
Upvotes: 2