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

Relation#from to accept other Relation objects #6058

Merged
merged 1 commit into from May 17, 2012
Merged

Relation#from to accept other Relation objects #6058

merged 1 commit into from May 17, 2012

Conversation

RStankov
Copy link
Contributor

I had to use subqueries in my project, several times. And I used this pattern:

Record.from("(#{sub_query.to_sql})")
Record.from("(#{sub_query.to_sql}) a")

Which doesn't feel right. I much prefer just to pass ActiveRecord::Relation objects:

Record.from("(#{sub_query.to_sql})")    -> Record.from(sub_query)
Record.from("(#{sub_query.to_sql}) a")  -> Record.from(:a => sub_query)

So this pull request add this functionality.

@larzconwell
Copy link
Contributor

👍

@jeremyf
Copy link
Contributor

jeremyf commented Apr 29, 2012

The tests all pass for this pull request.

@rafaelfranca
Copy link
Member

cc/ @jonleighton @tenderlove

@jonleighton
Copy link
Member

The seems good in principle. Some comments:

  • Needs documentation
  • I'm not keen on the Record.from(:a => sub_query) API. Would prefer Record.from(sub_query, :a) so the optional second parameter is an alias.
  • I don't think the build_from stuff should be done when #from is called - it should happen when we build the arel.
  • Needs a rebase

def build_from(opts, name = nil)
case opts
when Relation
opts.arel.as(name || "#{table_name}_2")
Copy link
Member

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"?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@RStankov
Copy link
Contributor Author

RStankov commented May 6, 2012

@jonleighton

I had:

  • Added documentation
  • Made subquery alias name as second optional argument
  • Made subquery default alias to be "subquery"
  • rebased

The only thing left todo, is to postpone build_from to be called in build_arel. Unfortunately I'm not very sure which is the best way to do this. But I will probably figure this out tomorrow.

@steveklabnik
Copy link
Member

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
Copy link
Contributor

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

@tenderlove
Copy link
Member

One more rebase please.

I like this feature. I'll apply if @jonleighton is happy too!

@jonleighton
Copy link
Member

I like it too. But I think we need to get the build_from logic into build_arel before applying :)

@RStankov
Copy link
Contributor Author

New rebase :)

I have moved build_from into build_arel ( I actually liked more, the version where build_from was used in from!)

I can do a little bit more cleanup there. Just want to check if you approve this approach.

@jonleighton
Copy link
Member

Looks good to me. The reason I wanted to do it in build_arel is because it makes the overhead of converting the from relation lazy. So if we don't need it in the end, then we won't have to do that work.

@jonleighton
Copy link
Member

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)
@RStankov
Copy link
Contributor Author

I'm done

jonleighton added a commit that referenced this pull request May 17, 2012
…relation-objects

Relation#from to accept other Relation objects
@jonleighton jonleighton merged commit 77a4072 into rails:master May 17, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

9 participants