Under the Hood
As shown earlier, each time you submit a URL, you’re creating an HTTP request, which fires a controller action. Any MVC framework designer needs to decide between reusing the same controller for each request and creating a new controller copy per request. Rails uses the latter strategy, which it calls request scope. Each HTTP request results in a new controller instance, meaning that you’ll also get a new set of instance variables for each HTTP request. That’s going to affect you in at least two different ways:
On the plus side, you don’t need to worry about threading in your controllers because each request gets a private copy of the controller’s instance data.
On the minus side, it will be harder for you to share instance data between requests, and Rails will take more resources. Specifically, if you set instance variables in one controller action method, don’t expect to be able to use them in later HTTP requests, even if you simply redirect your request. You’ll need to share them in a session, a cookie, or the URL.
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access