How to create an object in environment.rb and use it to shar


#1

Hi,

I was using a global ($) variable for this, but it seems global
variables remain loaded all the time that webrick is running, and I need
a fresh object for each HTTP request.

So I was trying to use the request.env array to add my own data there,
but it seems that that object isn’t created in environment.rb, so I
cannot use that approach. I need to create an object with some default
values on environment.rb, and then each controller can override any of
its attributes.

Could you please help me out?

  • Ivan V.

#2

ivanvega wrote:

Hi,

I was using a global ($) variable for this, but it seems global
variables remain loaded all the time that webrick is running, and I need
a fresh object for each HTTP request.

So I was trying to use the request.env array to add my own data there,
but it seems that that object isn’t created in environment.rb, so I
cannot use that approach. I need to create an object with some default
values on environment.rb, and then each controller can override any of
its attributes.

Could you please help me out?

  • Ivan V.

Hey Ivan,

As newby-on-rails, I don’t know if this suggestion will be of any help
to you, but for what it’s worth…

Because I will soon need to figure out how to do what is commonly
referred to as an “application variable” in other languages… (i.e. a
variable that all open sessions have access to).

I started doing some research & ran across something called “PStore”.

It’s described as a way of storing session information in a file in the
tmp dir… I know this is not what you are trying to do, but since it’s
possible, I assume, to read & write to this file from anywhere in the
app (I think read & write locking is built-in), perhaps you could use it
for your purposes?

I haven’t figured out how to use it yet, but here’s a wiki entry that
mentions it as well as other approaches;

http://wiki.rubyonrails.com/rails/pages/HowtoChangeSessionStore/

I hope you get a more useful answer from someone who knows far more than
I…

Randy


#3

Hey,

Thanks for the input. Unfortunately, as you point out, that’s not what
I’m trying to do, because I need a clean object variable each time a
page is requested, so a session/file wouldn’t work (well it could, but
it would be very complicated and way overkill). But I’m thinking that
might be of use for another area of my project!

I believe this is only an issue with webrick, because the application is
running the whole time, so I suppose Apache wouldn’t have this problem
because it runs the script from scratch for each request, I think…

Let’s hope someone can help me out!

Ivan V.


#4

Ezra Z. wrote:

attr_writer :foo
think this is what you are trying to do. Let me know if you need more
help.

Cheers-

-Ezra

Thanks a lot Ezra, that’s just what I was looking for.

Ivan V.


#5

Ivan-

You can place a before filter in your application.rb that gets

initialized to whatever you want on each request. Like htis:

class ApplicationController < ActionController::Base

before_filter :setup_foo

attr_reader :foo
attr_writer :foo
def setup_foo
   @foo = {:a => 'foo, :b => 'bar', :c => 'qux'}
end

end

Now @foo will be available to all your controllers and views. And

the attr_reader and attr_writer will set it up so your controllers
can modify foo per request. But @foo will be reset on each request. I
think this is what you are trying to do. Let me know if you need more
help.

Cheers-

-Ezra

On Nov 18, 2005, at 4:57 PM, Iván Vega R. wrote:

have this problem because it runs the script from scratch for each

values on environment.rb, and then each controller can override
As newby-on-rails, I don’t know if this suggestion will be of any
but since it’s possible, I assume, to read & write to this file

Randy


Rails mailing list
removed_email_address@domain.invalid
http://lists.rubyonrails.org/mailman/listinfo/rails

-Ezra Z.
Yakima Herald-Republic
WebMaster
http://yakimaherald.com
509-577-7732
removed_email_address@domain.invalid