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
Hi
I have a attachment model and I use upload_column to upload files.It
is working …Now In ServiceDeskAttachment class i have
upload_column :attachment, :store_dir => proc{|inst, attachment|
“uploads/servicedesk/#{@company_name}/#{inst.id}”} # This is not
working
My controller code for attachment is
@service_desk_attachment1=
ServiceDeskAttachment.new(params[:service_desk_attachment1])
@service_desk_attachment1.company_name=session[:company] #this i
tried like from one of above reply @service_desk_attachment1.save
The in ServiceDeskAttachment class
def set_company(company)
@company_name=company
puts ‘in set_company’+ company_name.to_s #Value I get here
end
But @company_name how can I set this to the above upload_column so
that it looks like
upload_column :attachment, :store_dir => proc{|inst, attachment|
“uploads/servicedesk/company1/#{inst.id}”}
methods.each do |shenanigan|
oops = instance_variable_get(:"@_#{shenanigan}")
klasses.each do |klass|
klass.send(:define_method, shenanigan, proc { oops })
end
end
yield
methods.each do |shenanigan|
klasses.each do |klass|
klass.send :remove_method, shenanigan
end
end
end
Now continue using the session variable in model. It works for me.
Thanks,
Sadeesh.
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?
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
–
Rails training from David A. Black and Ruby Power and Light:
Advancing With Rails August 18-21 Edison, NJ
Co-taught by D.A. Black and Erik Kastner
See http://www.rubypal.com for details and updates!
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.
Hi
The code is not a joke .It is working anyway…And I tried as you
Sorry. I assumed it was a joke, and not a very nice one, because of
the whimsical method and variable names (like “you_dont_have_a_clue”
and so forth). If it’s not a joke, then I would still say it’s not a
good idea to try to make the model automatically aware of the concept
of “session”.
David
–
Rails training from David A. Black and Ruby Power and Light:
Advancing With Rails August 18-21 Edison, NJ
Co-taught by D.A. Black and Erik Kastner
See http://www.rubypal.com for details and updates!
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?
Sorry. I assumed it was a joke, and not a very nice one, because of
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.
I take back the “not a very nice one” thing, which had to do with my
thinking that the method and variable names were being hurled at a
particular person. I’m not sure “nice” describes the original, but
neither does “not nice.” Its niceness is nil, rather than false
(And the message certainly looks clear to me.)
David
–
Rails training from David A. Black and Ruby Power and Light:
Advancing With Rails August 18-21 Edison, NJ
Co-taught by D.A. Black and Erik Kastner
See http://www.rubypal.com for details and updates!
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:
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
On Tue, Jul 29, 2008 at 5:36 PM, David A. Black [email protected]
wrote:
thinking that the method and variable names were being hurled at a
Co-taught by D.A. Black and Erik Kastner
See http://www.rubypal.com for details and updates!
methods.each do |shenanigan|
oops = instance_variable_get(:"@_#{shenanigan}")
klasses.each do |klass|
klass.send(:define_method, shenanigan, proc { oops })
end
end
yield
methods.each do |shenanigan|
klasses.each do |klass|
klass.send :remove_method, shenanigan
end
end
end
Now continue using the session variable in model. It works for me.
Thanks,
Sadeesh.
If I had a programmer working on my team and I ran across code like
this, well he wouldn’t be working on my team long. Just saying.
I am firmly rooted in the camp where model code is decoupled from
external data such as session, request or whatever else is the
responsibility of the View or Controller layers.
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
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.