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
Relation#from to accept other Relation objects #6058
Relation#from to accept other Relation objects #6058
Conversation
👍 |
The tests all pass for this pull request. |
The seems good in principle. Some comments:
|
def build_from(opts, name = nil) | ||
case opts | ||
when Relation | ||
opts.arel.as(name || "#{table_name}_2") |
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.
Not sure about the default alias here. Presumably we need to have one? How about just calling it "subquery"?
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.
Done
I had:
The only thing left todo, is to postpone |
Needs another rebase, apparently. |
# # => SELECT title FROM (SELECT * FROM topics WHERE approved = 't') subquery | ||
# | ||
# Topics.select('a.title').from(Topics.approved, :a) | ||
# # => SELECT a.title FROM (SELECT * FROM topics) WHERE approved = 't') a |
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.
Remove additional parentheses:
# => SELECT a.title FROM (SELECT * FROM topics) WHERE approved = 't') a
to:
# => SELECT a.title FROM (SELECT * FROM topics WHERE approved = 't') a
One more rebase please. I like this feature. I'll apply if @jonleighton is happy too! |
I like it too. But I think we need to get the |
New rebase :) I have moved I can do a little bit more cleanup there. Just want to check if you approve this approach. |
Looks good to me. The reason I wanted to do it in |
When you're done, please squash everything into a single commit and let us know so we can merge. Thanks. |
Record.from("(#{sub_query.to_sql})") -> Record.from(sub_query) Record.from("(#{sub_query.to_sql}) a") -> Record.from(sub_query, :a)
I'm done |
…relation-objects Relation#from to accept other Relation objects
I had to use subqueries in my project, several times. And I used this pattern:
Which doesn't feel right. I much prefer just to pass ActiveRecord::Relation objects:
So this pull request add this functionality.