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

as_json works differently for preload and eagerload #24314

Closed
salmanasiddiqui opened this issue Mar 25, 2016 · 10 comments
Closed

as_json works differently for preload and eagerload #24314

salmanasiddiqui opened this issue Mar 25, 2016 · 10 comments

Comments

@salmanasiddiqui
Copy link
Contributor

Steps to reproduce

class Portfolio < ActiveRecord::Base
  has_many :tickets
end
class Ticket < ActiveRecord::Base
  belongs_to :portfolio
end

@tickets = Ticket.includes(:portfolio).select('tickets.*', 'SUBSTRING(tickets.title,1,4) as code')
@tickets.first.as_json
@tickets.references(:portfolio).first.as_json

Expected behavior

@tickets.first.as_json should be equal to @tickets.references(:portfolios).first.as_json

Actual behavior

@tickets.first.as_json includes the code (alias column defined in the select)
@tickets.references(:portfolios).first.as_json does not include the code (alias column defined in the select)

Note

The problem arises only when eager_load is called by includes, preload works fine. More to that, using joins works fine aswell.

System configuration

Rails version: 4.2.6

Ruby version: 2.1.8

@vipulnsward
Copy link
Member

@salmanasiddiqui I did not get the use of references in this context? Its not being used.
The result of references vs includes is a bit different. In the former we need to construct results based on aliases, and don't happen to use select clauses, unlike includes, which is causing the difference.

@salmanasiddiqui
Copy link
Contributor Author

I just wanted to show that eager_load and preload are producing different results. If we dont use references, instead use where(portfolios: {id: [3,2]}) the result will be same. As Rails will also use the eager_load instead of preload in that case.
are you saying that this is the correct behavior? Is selectignored incase when includes results in LEFT JOIN? Is this documented somewhere?

@vipulnsward
Copy link
Member

Hi @salmanasiddiqui this isn't documented, but a known limitation. Going to mark this as a documentation issue.

@salmanasiddiqui
Copy link
Contributor Author

Known limitation, u mean that this cant be fixed and there isnt any workaround aswell?

I don't think that documenting it, is enough. It should also log a warning that select is being ignored in such scenarios.

@sgrif
Copy link
Contributor

sgrif commented Mar 31, 2016

This isn't even a limitation. This is the expected behavior. When you put an additional field in the select column, it is accessible through the record. I don't think this warrants a mention explicitly on as_json, and the behavior of select is thoroughly documented.

@sgrif sgrif closed this as completed Mar 31, 2016
@vipulnsward
Copy link
Member

@sgrif the issue is that select isn't returning extra selected values in case of eager load.

@sgrif
Copy link
Contributor

sgrif commented Apr 1, 2016

Ah, I see now. It's a very confusingly set up report...

@sgrif sgrif reopened this Apr 1, 2016
@salmanasiddiqui
Copy link
Contributor Author

workaround for anyone else facing this issue

replace includes with preload and use joins

example:
@tickets = Ticket.preload(:portfolio).joins('LEFT JOIN portfolios on portfolios.id = tickets.portfolio_id').select('tickets.*', 'SUBSTRING(tickets.title,1,4) as code').order('portfolios.name')

@al2o3cr
Copy link
Contributor

al2o3cr commented Apr 12, 2016

@sgrif Is this the same issue seen in #12423 and #15185?

@sgrif
Copy link
Contributor

sgrif commented Apr 12, 2016

Looks like it, yes.

niels added a commit to herimedia/wice_grid that referenced this issue May 1, 2016
vipulnsward added a commit to vipulnsward/rails that referenced this issue Aug 9, 2016
… eager loading causing it to ignore custom select clauses.

[ci skip]

Fixes rails#24314
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants