djburdick
djburdick

Reputation: 11940

ruby on rails way to destroy extra session variables in a model?

I have additional variables I add to an authlogic users session like:

session[:current_profile] = extra_id

I currently destroy these on logout in a controller like:

session[:current_profile] = nil

I'd like to clean this up and destroy them in the session model in the after_destroy method like:

def after_destroy
   session[:current_profile] = nil
end

This session method doesn't seem to be callable from the models though. Any idea how to destroy a session variable from a model?

Thanks!

Upvotes: 1

Views: 474

Answers (1)

tadman
tadman

Reputation: 211540

You're really not supposed to alter things in the Controller space from the Model space, which is to say, a Model should not be controlling a Controller. Models should be able to run independently of a controller, such as in unit tests where no controller is present.

While you might be able to wrangle this sort of thing with an Observer, I don't know of an easy way to do this. It's probably better to have the controller perform all the required actions directly.

If you put an after_destroy hook like this in you will have some serious side-effects if, for example, a user logged in as an admin destroys another user account and then their session profile suddenly disappears.

Upvotes: 2

Related Questions