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

to_prepare not called on every development request #7152

Closed
sigmike opened this issue Jul 25, 2012 · 12 comments
Closed

to_prepare not called on every development request #7152

sigmike opened this issue Jul 25, 2012 · 12 comments

Comments

@sigmike
Copy link

sigmike commented Jul 25, 2012

Like in this stackoverflow question, my to_prepare block is called only once in development.
The Rails guide says it should be called before every request in development. Is the guide wrong?
I illustrated the issue here: ouvrages@8841e2a based on 3-2-stable. The output is <2> expected but was <1>

@jfirebaugh
Copy link
Contributor

I suspect your test case is failing because config.cache_classes is true in tests.

@rafaelfranca
Copy link
Member

Yes, if cache_classes is true so the to_prepare block will be called once. Could you confirm if this is causing the issue?

@carlosantoniodasilva
Copy link
Member

@piglop any more info to help us on that? Otherwise we'll consider this closed, thanks!

@zhelee
Copy link

zhelee commented Aug 26, 2012

@carlosantoniodasilva I meet the same problem. I am in development mode, so config.cache_classes = false, I just use

config.to_prepare do
  binding.pry
end

to test whether it reloads on every request, but it only executes when I start the rails server, after that, it never executes again

@xiaods
Copy link
Contributor

xiaods commented Aug 29, 2012

the guide description is wrong, the behavior is keep consistent to load once in my testing project.

@carlosantoniodasilva
Copy link
Member

It's not that the guide description is wrong, it does reload on every request in development, but it's tied to the classes reloader: if you don't change anything that's reloadable, ie a model, controller, view, route, or similar, it won't be triggered. If you do change anything under app/, or your routes, then to_prepare will be called for every request that happens to have a change - or every request that needs to reload code.

I have to check with @josevalim and see if and how we're going to change the docs, but that's how it works since 3.2.

@carlosantoniodasilva
Copy link
Member

Also important to link the related commit: fa1d9a8. See the commit description there for more info, and note that you can set config.reload_classes_only_on_change to false to get back 3.1 behavior.

@josevalim
Copy link
Contributor

Yes, we need to fix the docs. to_prepare is called every time your project is reloaded, which since Rails 3.2 means every time you change a file.

@arturopie
Copy link
Contributor

Was the documentation fixed? I think it still has the error.

y-yagi added a commit to y-yagi/rails that referenced this issue Oct 26, 2016
`ActionDispatch::Callbacks.to_prepare` was deprecated in d3c9d80.
In addition, `to_prepare` events does not call every request.
`to_prepare` events call every time application is reloaded.
Ref: rails#7152
@jpowell
Copy link

jpowell commented Jan 6, 2020

Bump - this is still incorrect in Rails docs... yeeeeeears later

@simi
Copy link
Contributor

simi commented Jan 6, 2020

@y-yagi any reason why your fix (y-yagi@5fb9cf1) wasn't merged?

@y-yagi
Copy link
Member

y-yagi commented Jan 6, 2020

Hmm. Sorry, I couldn't remember why I didn't create a PR. If anyone interested to improve a doc, feel free to create a PR.

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