can I access session variable in model

Hi    can I access session variable in model? I have in the user controller code session[:site_id] = @the_site.site_id

And how can i access this in one of my active record model SDAttachment?I tried like site_id=session[:site_id] but this gives error. Please help

Sijo

no, you can't

If you need information from the session hash in a model, you must hand it to the models method like:

def do_something(session_id) ... end

and call that from the controller like

@my_model.do_something(session[:site_id])

Thorsten Mueller wrote:

no, you can't

Well, you _can_, but it's not as straight forward as you'd like. You can use Thread.current and store things similarly as you do with session. I don't know the ramifications of doing this with very much data, though. This thread

mentions it. A Google search will probably turn up a bit of useful information.

Peace, Phillip

Not to mention it completely violates MVC. So even if you find out how... It's not a good idea.

RSL

Yes you could access the session variable in model. Paste the below code in application.rb

Hi      Lots of thanks for your help.It is working now..But what actaully happened? I have the curiosity to know that..Is it anything against the MVC architecture? And will it create any future problems during deployment like that?

Thanks Sijo

Hi,    Probably no problem comes.I am not sure. Its your responsibilty. And you could access all these variables there    ["session", "cookies", "params", "request"] .

   Thanks,    Sadeesh.

Hi --

Hi     Lots of thanks for your help.It is working now..But what actaully happened? I have the curiosity to know that..Is it anything against the MVC architecture? And will it create any future problems during deployment like that?

If you're talking about the code with "you_dont_have_bloody_clue" and so forth, don't use it. It was some kind of joke, I believe.

The model has no concept of a "session". It shouldn't be able to tell whether it's being accessed from a controller or the console or a standalone script. It's only the controller that knows about sessions.

As another respondent said, if you need the model to have session data, you should write your model method to take an argument and then pass the relevant session data in. The model doesn't know where the method argument comes from, and it shouldn't know.

David

Hi      The code is not a joke .It is working anyway..And I tried as you said.(5 th post above)..In the model ServiceDeskAttachment i made a attr_accessor :company And from controller I wrote @sd_attachment=ServiceDeskAttachment.new(params[:attachment_data]) @sd_attachment.company=session[:company] @sd_attachment.save

And in ServiceDeskAttacment model I I have the following code at top upload_column :attachment, :store_dir => proc{|inst, attachment| "uploads/servicedesk/#{@company}/#{inst.id}"}

But this does not get @company value...If I get value there, then my problem is solved..Could you please tell me how can this be solved?

Thanks in advance sijo

The whole thing is just not a good idea. If your model needs external data, then pass it in. When classes have direct access to things that do not belong to them then you're building a house of cards.

If you have a model that needs some external data, then pass it in as an argument.

Hi --

http://m.onkey.org/2007/10/17/how-to-access-session-cookies-params-request-in-model

I don't think the message could have been any cleaerer. But if it helps. let me say it again as the original author of that code : DO NOT USE IT.

Hi --

Hey David,

Sorry, I didn't mean to quote you ( just pressed gmail reply button ). My message was not for you, but for the people using the code :slight_smile:

Hi,

I have a scenario where the session variable is handy in the model - I need to be able to get the current value, as it may be changed at any time.

I have a delayed_job that runs a long running model activity (calling multiple web services, populating the database, etc.) in the background. This is triggered when a user selects an item from a list in the UI. However, if the user clicks on another item, I want the current processing to stop, as I want to keep the delayed_job worker pool as free as possible (the more the jobs, the more I am billed by the hoster). The only way I can see to achieve this is to set a session variable (session[:current_topic_id]) when the user clicks on a topic, and then check in each stage of the model action if the current topic is the same as the one stored in the session, and abort the long job if they are out of sync.

Here, passing in the session variable to the model method wont really work for me, I have to pass in the session id and check the variable value across the model code.

Thanks Anand

Pratik Naik wrote:

I tried passing the session id around to the model, and agree that it is very ugly.

I am curious to know how you can best achieve this scenario without passing in the session variable: abort a delayed_job (or other model based) work item when user changes something - this is mainly required because delayed job workers cost money at my host (heroku) and if the user randomly clicks around on the UI, I dont want to be billed for all the spawned jobs that no one cares about anyway.

Thanks

Anand

We can do that with attr_accessor

In model attr_accessible :site_id attr_accessor :site_id

in form, add a hidden field like this

=f.site_id, value; session[:site_id]

in model we can use self.site_id now.