Ruby on Rails Friday, July 14, 2017

On Friday, July 14, 2017 at 9:40:56 AM UTC+1, Ralph Shnelvar wrote:

class User < ActiveRecord::Base
 def self.any_users?
   @any_users ||= exists?

So at some point in the code, I call
So Ruby says "Ok, @any_users isn't defined so I'm going to call exists?"  That makes perfect sense.

Let's say I have several people banging on my website.  Do each of them get a completely different copy of Rails?  How many copies of @any_users are there?  Does @any_users get initialized for each user?  What data is common for all
threads? What's different for all threads?
Where can I learn more about this?  I've skimmed but it really isn't helping me understand what is and isn't common between each thread/user/session.

It depends on how you serve your rails app. For some setups you have many processes, each process handles a request at a time. Each process holds a completely separate copy of your rails application. Others use one process with many threads. This is one process, so there is only one copy of your rails app in memory. Some setups are a hybrid (many processes, each of which serving multiple concurrent requests).

As for the scope of you data, first off there isn't really such a thing as session scope. global / class variables are shared across all threads. If the data is an instance variable of a User, then it's specific to that user (note though that in the code you post @any_users is a instance variable of the User class (not an individual user), so it is shared.


You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to
To post to this group, send email to
To view this discussion on the web visit
For more options, visit

No comments:

Post a Comment