sergserg
sergserg

Reputation: 22264

How to access session information from within model?

As this person so eloquently put it:

You’re completely [...] wrong if you want to access sessions, params, cookies, etc. in your Models.

However, I think I have a legitimate case for this - and if not please let me know of a different approach.

I'm using the KeenIO api to publish an event when a user registers on the website. Not logs in, but registers. A brand new user.

# Controller code:
@user = User.find_or_create_for_github_oauth(request.env["omniauth.auth"], current_user)

# Model method:
user = User.find_by(email: auth_hash['info']['email'])
if user.nil? # User is brand new, bla bla bla.
  Keen.publish(:account_registration_au, { user_session: { session_id: session.id } })
end

How can I access the session information from within my model? I'm trying to create a Keen Funnel and I need that session ID to track the user through my funnel steps.

Upvotes: 2

Views: 3072

Answers (1)

BroiSatse
BroiSatse

Reputation: 44715

I like describing MVC pattern with a following image:

So, you are to write some web application. No doubts, the most important part of any application is data - web application without any data is just a static website. Hence your data is what makes your website unique and will be the first reason why the customers will visit it.

Now imagine your data being some weird animals living on a far far away planet - there is a whole, small eco-system living there: huge articles are walking slowly, with a bunch of comments running under their feets, authors are feeding on upvotes, etc. This is the domain of your application - this is the truth. Model is the truth. And you are like god here.

Now you are building your application in that world. You have to build a small village in it. It will have its own post office (the server), and couple of small buildings called controllers. When the new request comes in, your post office will read its address and pass it to the right controller. Now the people in the controller will read the message and will decide that they need some data to process it - the hunting team is called and they go to the jungle to catch it. (Actually they just make a photo of it, unless you enforce pessimistic locking, long story). This data is processed and passed to controller secretary, who pastes this processed data (with absolutely no knowledge of what it is - it is not his job) to a ready template. This then get back to a post office to be sent back as a response.

Important things to notice here:

  1. Your controllers are heart of your application, hidden in a vast plain of your model.
  2. Models do not care for your application. They would live as they live with or without it.
  3. You do not want some goats to wonder around your shiny office, do you?

Passing application-specific data to model is fine, if you want to shape existing ecosystem. However, giving models an access to application controls like session or cookies are extremely risky. Passing controller instance to a model is like sending director of the zoo to feed the wild tigers.

Apart fro that, feeding the models with extra application data will make them application dependent and they will not survive with it, when you for example open the console.

Upvotes: 2

Related Questions