-
Notifications
You must be signed in to change notification settings - Fork 21.7k
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
added ActiveRecord::Relation#left_outer_joins #12071
Conversation
+1 |
2 similar comments
+1 |
+1 |
+1 @Crunch09 Your patch is so much more pro than my poor man's outer_join where I gsub the inner joins to outer joins. https://gist.github.com/njakobsen/6395146 |
+1 |
Thanks for your contribution @Crunch09! Actually, it would be awesome if we could update a little bit the guides with this new feature. |
yes, will write something tomorrow |
@@ -948,6 +983,11 @@ def build_joins(manager, joins) | |||
end | |||
end | |||
|
|||
build_join_query(manager, buckets, Arel::InnerJoin) | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
✂️
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thank you, fixed that!
+1 |
1 similar comment
+1 |
@robin850 Added few sentences in the AR-querying guide about it and also rebased against current master. |
@Crunch09 : Awesome, thank you! ❤️ @rafaelfranca : Could you please have a look ? It looks awesome to me! |
@rafaelfranca @robin850 I just rebased again against current master, anything more i can do? |
+1 Outer joins is one of the few remaining things that force us to fall back to SQL in our codebase |
build_join_query(manager, buckets, Arel::InnerJoin) | ||
end | ||
|
||
def build_join_query(manager, buckets, join_type=nil) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please do not default join_type
to nil
. I don't think nil
is actually an acceptable value. Change the things that call build_join_query
to actually pass in the things it needs.
Can you please change these tests not to use |
I'm positive on this, but the copy/paste make me uneasy. :( |
@@ -937,11 +938,17 @@ This will result in the following SQL: | |||
SELECT clients.* FROM clients LEFT OUTER JOIN addresses ON addresses.client_id = clients.id | |||
``` | |||
|
|||
### Using Array/Hash of Named Associations | |||
Instead of writing raw SQL in this case you could also use the `outer_joins` method | |||
which would produce the same result. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we're going to introduce an outer_joins
, I think this is a bit of an "also-ran" position to mention it... we should probably be offering it before we suggest raw SQL.
In fact, maybe the SQL should change, to show a still-legitimate use case for a raw JOIN, as it previously did, instead of an inferior spelling of built-in functionality.
I'm vaguely wary of Maybe consider |
Good point. If it's actually going to do an LOJ, the method should probably be |
Thank you for your feedback! I'll update this PR over the weekend and report back then |
One thing I'd love to see but don't have a clear API for - the ability to specify additional bits in the
which would produce the SQL:
assuming there's a boolean Note that the above is not equivalent to:
since that query doesn't include users who don't have any featured posts. Without this feature, it would still be possible to create the desired SQL with a scoped association:
but that method doesn't exactly scale to arbitrary sets of conditions. |
@al2o3cr +1. We should probably address that separately, afterwards, though... whatever we end up with seems like it should apply to |
Any word on when this will be merged? It's been a long time in coming, and every project I make ends up needing left outer joins. |
it's my fault. There were lots of changes in the affected code and i need to solve the merge conflicts and also apply the feedback. I started with that and i hope i can finish it this weekend, sorry about the long wait. |
@k0kubun Good point! I would be fine either way. |
@al2o3cr have you already created separate issue for supporting the |
Doh. I needed a left outer join again today. |
Example: User.left_outer_joins(:posts) => SELECT "users".* FROM "users" LEFT OUTER JOIN "posts" ON "posts"."user_id" = "users"."id"
added ActiveRecord::Relation#outer_joins
This assumes only one query was ever executed, but it appears to sometimes be loading schema information. We can just look at the array of queries, rather than the "first" one that was run
🎉 |
👏 👏 |
Thank you all ❤️ So happy this got merged! |
👏👏 |
Hi,
this adds the ability to use a
left outer join
query without writing pure SQL as an argument to#joins
and also query for a specific selection of columns which doesn't work with#includes
.The ability to do this is already implemented in
ActiveRecord::Associations::JoinDependency#build
but it was only used withArel::InnerJoin
asjoin_type
.I am not quite sure about the tests, i just used the InnerJoin tests as a starting point. Any help would be appreciated.
Example: