Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Ecto.Query.has_named_binding?/2 #2738

Merged
merged 2 commits into from
Oct 9, 2018

Conversation

AndrewDryga
Copy link
Contributor

@AndrewDryga AndrewDryga commented Oct 9, 2018

Related to #2389.

The main use case is preloading an association if it's not already preloaded:

  def with_preloaded_account(queryable) do
    if has_named_binding?(queryable, :account) do
      queryable
    else
      queryable
      |> join(:left, [assignment], account in assoc(assignment, :account), as: :account)
      |> preload([assignment, account: account], account: account)
    end
  end

We may also add a doctest with example usage:


  ## Examples

        iex> import Ecto.Query
        iex> query = from(p in "posts", as: :posts, \
                join: b in "blogs", \
                join: l in "links", as: :link)
        iex> has_named_binding?(query, :posts)
        true
        iex> has_named_binding?(query, :link)
        true
        iex> has_named_binding?(query, :blogs)
        false

@josevalim josevalim merged commit 3e24a10 into elixir-ecto:master Oct 9, 2018
@josevalim
Copy link
Member

❤️ 💚 💙 💛 💜

@AndrewDryga AndrewDryga deleted the andrew/has_named_binding branch October 9, 2018 10:50
@OvermindDL1
Copy link
Contributor

Woooo! This was the last bit I needed to be able to get rid of all my state passing around and go back to base Ecto Query's and base Repo calls! Thanks much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants