-
Notifications
You must be signed in to change notification settings - Fork 11
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
[FeatureRequest] Support joined associations #22
Comments
I don't think accepting a hash for I have not personally tested this, but I think you should be able to write something like: Article.joins(:author).includes(:author).merge(Author.distinct_on(:id)) This strategy also works for |
Fair point. I guess i figured given it's related to associations, i could follow a similar interface to I completely overlooked using I do think if you want to remain consistent with select and order, |
I agree! I am surprised that does not work. It seems like a bug to me. |
Yeh it doesnt because the code only allows attributes on the current arel_table. I've taken a crude approach to it here. This satisfies my requirement to use distinct on with a joined association. |
I'm open to accepting strings for association support if anybody wants to provide a PR, e.g.: Author.joins(:books).distinct_on('books.title') I've needed this before quite a few times. |
It's currently not possible to use joined associations with
distinct_on
andorder
If you do, Postgres will error "SELECT DISTINCT ON expressions must match initial ORDER BY expressions"
So i tried to see if I can include the order expression as well, but it doesn't work...
Contrived example...
Article.joins(:author).includes(:author).order(author: :id).distinct_on(:id, author: :id)
This generates invalid SQL:
SELECT DISTINCT ON ("articles"."{:author=>:id}")
tried
Article.joins(:author).includes(:author).distinct_on('"authors"."id"')
This generates invalid SQL also
SELECT DISTINCT ON ("articles"."""authors"".""id""")
I realise this is a limitation of the gem, It would be nice if it could support this as currently it's not usable with any queries that order by a joins association.
I have added basic support for it in our fork.
residently@f8c6dc5
The text was updated successfully, but these errors were encountered: