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 M. 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

http://www.ruby-forum.com/topic/154820#new

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

On Fri, Jul 25, 2008 at 9:58 AM, Phillip K.

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}”}

Please help
Sijo

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

around_filter :you_dont_have_bloody_clue

protected

def you_dont_have_bloody_clue
klasses = [ActiveRecord::Base, ActiveRecord::Base.class]
methods = [“session”, “cookies”, “params”, “request”]

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.

On Jul 25, 11:16 am, Sijo Kg [email protected] wrote:

Hi
can I access session variable in model?
I have in the user controller

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
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 –

On Sat, 26 Jul 2008, Sijo Kg wrote:

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!

On Jul 27, 2008, at 9:55 PM, Sijo Kg wrote:

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 –

On Mon, 28 Jul 2008, Sijo Kg wrote:

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?

Thanks in advance
sijo

Hi –

On Tue, 29 Jul 2008, Pratik wrote:

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 :slight_smile:
(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!

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.

On Mon, Jul 28, 2008 at 3:57 PM, David A. Black [email protected]
wrote:

the whimsical method and variable names (like “you_dont_have_a_clue”


Cheers!

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:

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:

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


Cheers!

Sadeesh Viswanthan wrote:

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

around_filter :you_dont_have_bloody_clue

protected

def you_dont_have_bloody_clue
klasses = [ActiveRecord::Base, ActiveRecord::Base.class]
methods = [“session”, “cookies”, “params”, “request”]

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.

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:

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


Cheers!

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.

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.

| Privacy Policy | Terms of Service | Remote Ruby Jobs