AdamNYC
AdamNYC

Reputation: 20415

How does Rails distinguish instant variables that belong to different users

I am following a RailsCast on implementing Facebook API. In the User model, I have a method:

  def facebook
     @facebook ||= Koala::Facebook::API.new(oauth_token)

which creates an instant variable that can be shared with other methods within User model.

My question is:

If my Rails app has 100 users online, each of them create one instance @facebook, how does Rails know which @facebook is associated with with user?

Upvotes: 1

Views: 40

Answers (2)

Dave Newton
Dave Newton

Reputation: 160201

Instance variables are associated with the instance of the object they belong to.

Users interact with their own User instance because it's associated with their session or a specific request. Requests are processed using request-specific instances of controllers that in turn deal with a specific instance of a User, hence @facebook value.

Upvotes: 1

Femaref
Femaref

Reputation: 61437

It doesn't. There is no such thing as "online" in a web app, every time you click a link, a request a sent. For that request, all classes are instantiated, including running the code you posted.

After the request is completed, those classes are disposed of.

Upvotes: 1

Related Questions