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

has_many through while selecting extra columns breaks in 4.1 #14467

Closed
jshirley opened this issue Mar 24, 2014 · 3 comments
Closed

has_many through while selecting extra columns breaks in 4.1 #14467

jshirley opened this issue Mar 24, 2014 · 3 comments

Comments

@jshirley
Copy link

I have a relationship that includes selected columns (I'm honestly doubting whether this is a good idea at all now that I've ran into this), and it worked through 4.0.x. Updating the app to 4.1.0rc1 showed this behavior no longer works and results in an SQL error, as the select is not dropped from the query constructed.

Essentially, creating a relationship in this fashion (my actual use case is more sane, at least in theory, separating two very distinct classifications of relationships but also wanting to get the join table properties as well):

  has_many :prime_categories,
    -> {
      includes(:post_category)
      .references(:post_category)
      .where(post_categories: { prime: true })
      .select("post_categories.id, categories.*")
    },
    through: :post_categories,
    source: :category

In the gist I also have commented out the explicit includes and references, as they don't make a difference in 4.1.

Failing executable gist:
https://gist.github.com/jshirley/9744173

Note: This example does pass with activerecord 4.0.0 activated (the select is not included in the query).

@rafaelfranca
Copy link
Member

Yes, This behavior changed and it is documented in the CHANGELOG. The reason is sometime someone want to select being applied (and it make sense) and how it worked before doesn't allow this. Now select are applied unless you choose to not.

@jshirley
Copy link
Author

Can you confirm that the correct fix in the app to include .count(:all)?

This wasn't adequately explained in the CHANGELOG (in fact, searching for select in the 4.1 CHANGELOG doesn't yield any results at all). (Though it seems this is the right PR: #10710)

@rafaelfranca
Copy link
Member

@jshirley yes, it is exactly this change. The CHANGELOG is there and it mentions select

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

No branches or pull requests

2 participants