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.
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?
Hi,
Probably no problem comes.I am not sure. Its your responsibilty.
And you could access all these variables there
["session", "cookies", "params", "request"] .
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.
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?
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.
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.
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.