Where by field

Ecto for Beginners : part 10 of 22 published on Sep 06, 2015
# /test/models/posts/where_title_test.exs
defmodule Readdit.Posts.WhereTitleTest do
  use ExUnit.Case
  use Readdit.ConnCase
  import Ecto.Query

  setup do
    %Readdit.Post{title: "Han Shot First"} |> Readdit.Repo.insert!
    %Readdit.Post{title: "Han Shot First"} |> Readdit.Repo.insert!
    %Readdit.Post{title: "R2D2 is the best!"} |> Readdit.Repo.insert!
    %Readdit.Post{title: "Rosetta Stone for Wookies"} |> Readdit.Repo.insert!

    :ok
  end

  test "where title |> all" do
    posts = Readdit.Post
    |> where([p], p.title == "Han Shot First")
    |> Readdit.Repo.all

    assert Enum.count(posts) == 2
  end

end
Next: Where with ranges of values