title: Query records before insertion with Ecto (similar to an AR callback)
body: <p>I'm new to Elixir and Phoenix (less than 10 days) but very excited about it and like many others I come from a Rails background.</p>
  
  <p>I understand Ecto is not AR and callbacks have been deprecated or removed but I need to add a custom validation that should only happen on creation and needs to perform a query.</p>
  
  <p>Here's what my <code>Reservation</code> model basically looks like.</p>
  
  <pre><code>schema "reservations" do
    field :ends_at, :utc_datetime
    field :name, :string, null: false
    field :starts_at, :utc_datetime
    field :user_id, :id
  end
  </code></pre>
  
  <p>and then I have another schema <code>Slot</code>, which looks like this:</p>
  
  <pre><code>schema "slots" do
    field :ends_at, :utc_datetime
    field :name, :string, null: false
    field :starts_at, :utc_datetime
    field :admin_id, :id
  end
  </code></pre>
  
  <p>Whenever I'm adding a new reservation, I need to query my DB to check if there are any slots with matching <code>ends_at</code> and <code>starts_at</code>. If there are, I need to prevent the record from being saved and add an error to it (similar to what in Rails we accomplish with <code>throw :abort</code> and <code>errors.add</code>).</p>
  
  <p>Can someone please shed a light on this? What's the Ecto way of doing this?</p>
  
  <p>Best regards</p>