Maintaining non persisted data

Hi all,

I have the following file below which is a controller inteneded to
maintain non persisted data. It has two routes defined by ‘gather’ and
‘show’. Gather displays a form and show, obviously, shows the non
persisted data. My problem is that initialize is called every time
gather and show are called causing me to lose the data contained in @td.
How do I fix this?

Thanks,

Cris

class TransientController < ApplicationController

def initialize
@td = TransientData.new
puts ‘initialize called’
end

def gather
puts ‘gather’
respond_to do |format|
format.html # gather.html.erb
end
end

def show
puts 'show ’
@td.firstname=(params[:firstname])
@td.lastname=(params[:lastname])
respond_to do |format|
format.html #show.html.erb
end
end

end

class TransientData

def initialize
@firstname=‘default1’
@lastname=‘default2’
end
attr_accessor :firstname, :lastname
end

Cris S. wrote:

I tried with statics

(They’re just called “class variables” in Ruby.)

and that did not work. It is as if rails
completely unloads the controller between invocations.

In development mode, it does. And in production, there’s no guarantee
that you’ll get the same server instance as last time, so you can’t
really rely on class variables in the controller. You might be able to
rig up something in a model…

But this smells funny. What exactly are you trying to do here? In most
cases, if data is worth saving, it’s worth putting in the DB.

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

I tried with statics and that did not work. It is as if rails
completely unloads the controller between invocations.

class TransientController < ApplicationController

def initialize
puts ‘initialize called’
end

def gather
puts ‘gather’
respond_to do |format|
format.html # index.html.erb
end
end

def show
puts 'show ’
TransientData.firstname=(params[:firstname])
TransientData.lastname=(params[:lastname])
respond_to do |format|
format.html #show.html.erb
end
end

end

class TransientData

@@firstname=‘default1’
@@lastname=‘default2’

def self.firstname()
@@firstname
end

def self.lastname()
@@lastname
end

def self.lastname=(name)
@@lastname = name
end

def self.firstname=(name)
@@firstname=name
end

end

Cris S. wrote:

Hi all,

I have the following file below which is a controller inteneded to
maintain non persisted data. It has two routes defined by ‘gather’ and
‘show’. Gather displays a form and show, obviously, shows the non
persisted data. My problem is that initialize is called every time
gather and show are called causing me to lose the data contained in @td.
How do I fix this?

Thanks,

Cris

class TransientController < ApplicationController

def initialize
@td = TransientData.new
puts ‘initialize called’
end

def gather
puts ‘gather’
respond_to do |format|
format.html # gather.html.erb
end
end

def show
puts 'show ’
@td.firstname=(params[:firstname])
@td.lastname=(params[:lastname])
respond_to do |format|
format.html #show.html.erb
end
end

end

class TransientData

def initialize
@firstname=‘default1’
@lastname=‘default2’
end
attr_accessor :firstname, :lastname
end

Cris S. wrote:

Hi Marnen.

Thanks for your reply. Here is what I am trying to do…

We are trying to use mongrel as a job execution engine and use rails for
the front end GUI.

I’m not sure I understand. Mongrel is just a Web server. Are you
saying you have a Rails app that calls other processes?

We intend to have an initial configuration screen
where the administrator will populate various properties in form for use
in determine how the various jobs will run. Whenever the ‘system’ (and
system will consist of more than, but include, mongrel) is brought down
for maintenance we want the admins to reexamine these properties so we
want them ‘forgotten’ between maintenance windows. Scalability is not
an issue here as only a handful of admins will use the tool, (thus no
clustering will be involved) and I expect statics will work.

Again: they’re not called that in Ruby. This isn’t Java. :slight_smile:

In fact,
when I moved the TransientData class to a file called library.pl

Did you mean library.rb ? (Anyway, it should be in a file called
transient_data.rb for clarity.)

in the
lib directory and required it in the controller it did (finally) work.

That makes some sense, I suppose…

If the right approach is to use an active record, despite the lack of
need for persistence, can one create an active record and declare it to
be a singleton (i.e. never more than one row in the database)? Can one
do the equivalent of a j2ee startup bean and have a mongrel run a job
upon startup of the server so I could delete that singleton? The idea
of being able to use an active record and have all of the validation
goodies does appeal to me.

You could do this (although there are a variety of reasons that
singletons are probably a bad idea), but if it’s just a question of
configuration parameters, it’s probably easier to read them from a YAML
file. There’s a Railscast on this topic.

If you really want them transient, though, there’s no need to store them
anywhere at all! Just call TransientData.new in the show action, not in
the constructor. Your controllers don’t need – and should not have –
constructor methods. Remember, this isn’t Java – instance variables
spring into existence whenever you mention them.

Thanks,

Cris

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

Marnen,

You are fast…

Class variables… got it! :wink:

I intend to spin off worker threads(or fork if need be) after the
initial ‘submit’ button is pushed.
Those workers will tend to the jobs at hand.

Yes I meant library.rb (not perl)…

I really do not want to use a yaml file. Some of the config data will
include passwords to other systems that the worker threads will need to
perform their duties. Also, the admins will get cranky if I make them
modify a yaml file.

If I use a ‘singleton’ active record I do need to figure out how to
encrypt the password in the database.

If I use a static, err class variable, I am more than willing to roll
the dice and assume no one will dump mongrel’s memory and hunt for it.

I am very willing to use the class variable approach, but I am highly
curious how to build a ‘singleton’ model to make use of the various
active record goodies (assuming I can delete it upon startup).

Thanks,

Cris

Marnen Laibow-Koser wrote:

Cris S. wrote:

Hi Marnen.

Thanks for your reply. Here is what I am trying to do…

We are trying to use mongrel as a job execution engine and use rails for
the front end GUI.

I’m not sure I understand. Mongrel is just a Web server. Are you
saying you have a Rails app that calls other processes?

We intend to have an initial configuration screen
where the administrator will populate various properties in form for use
in determine how the various jobs will run. Whenever the ‘system’ (and
system will consist of more than, but include, mongrel) is brought down
for maintenance we want the admins to reexamine these properties so we
want them ‘forgotten’ between maintenance windows. Scalability is not
an issue here as only a handful of admins will use the tool, (thus no
clustering will be involved) and I expect statics, I mean class variables, will work.

Again: they’re not called that in Ruby. This isn’t Java. :slight_smile:

In fact,
when I moved the TransientData class to a file called library.pl

Did you mean library.rb ? (Anyway, it should be in a file called
transient_data.rb for clarity.)

in the
lib directory and required it in the controller it did (finally) work.

That makes some sense, I suppose…

If the right approach is to use an active record, despite the lack of
need for persistence, can one create an active record and declare it to
be a singleton (i.e. never more than one row in the database)? Can one
do the equivalent of a j2ee startup bean and have a mongrel run a job
upon startup of the server so I could delete that singleton? The idea
of being able to use an active record and have all of the validation
goodies does appeal to me.

You could do this (although there are a variety of reasons that
singletons are probably a bad idea), but if it’s just a question of
configuration parameters, it’s probably easier to read them from a YAML
file. There’s a Railscast on this topic.

If you really want them transient, though, there’s no need to store them
anywhere at all! Just call TransientData.new in the show action, not in
the constructor. Your controllers don’t need – and should not have –
constructor methods. Remember, this isn’t Java – instance variables
spring into existence whenever you mention them.

Thanks,

Cris

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

[If you’re replying point by point, please try to quote the lines you
are responding to. It will make the discussion easier to follow.]

Cris S. wrote:

Marnen,

You are fast…

Class variables… got it! :wink:

:slight_smile:

[…]

I really do not want to use a yaml file. Some of the config data will
include passwords to other systems that the worker threads will need to
perform their duties. Also, the admins will get cranky if I make them
modify a yaml file.

If I use a ‘singleton’ active record I do need to figure out how to
encrypt the password in the database.

Perhaps. But you don’t need to take this approach.

If I use a static, err class variable, I am more than willing to roll
the dice and assume no one will dump mongrel’s memory and hunt for it.

I am very willing to use the class variable approach,

This is the wrong approach. If I understand the problem correctly, your
first approach (using instance variables) is almost correct – just get
rid of the initialize method as I explained above.

but I am highly
curious how to build a ‘singleton’ model to make use of the various
active record goodies (assuming I can delete it upon startup).

I really doubt that this is worth doing.

Thanks,

Cris

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

Cris S. wrote:

Hi all,

I have the following file below which is a controller inteneded to
maintain non persisted data. It has two routes defined by ‘gather’ and
‘show’. Gather displays a form and show, obviously, shows the non
persisted data. My problem is that initialize is called every time
gather and show are called causing me to lose the data contained in @td.
How do I fix this?

Thanks,

Cris

To summarize and extend what I said in previous posts: I don’t really
think you need to store this data at all. It doesn’t look like it lives
any longer than one request cycle. But if you do, it may be worth
storing in the session…

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

Hi Marnen.

Thanks for your reply. Here is what I am trying to do…

We are trying to use mongrel as a job execution engine and use rails for
the front end GUI. We intend to have an initial configuration screen
where the administrator will populate various properties in form for use
in determine how the various jobs will run. Whenever the ‘system’ (and
system will consist of more than, but include, mongrel) is brought down
for maintenance we want the admins to reexamine these properties so we
want them ‘forgotten’ between maintenance windows. Scalability is not
an issue here as only a handful of admins will use the tool, (thus no
clustering will be involved) and I expect statics will work. In fact,
when I moved the TransientData class to a file called library.pl in the
lib directory and required it in the controller it did (finally) work.

If the right approach is to use an active record, despite the lack of
need for persistence, can one create an active record and declare it to
be a singleton (i.e. never more than one row in the database)? Can one
do the equivalent of a j2ee startup bean and have a mongrel run a job
upon startup of the server so I could delete that singleton? The idea
of being able to use an active record and have all of the validation
goodies does appeal to me.

Thanks,

Cris

Marnen Laibow-Koser wrote:

Cris S. wrote:

I tried with statics

(They’re just called “class variables” in Ruby.)

and that did not work. It is as if rails
completely unloads the controller between invocations.

In development mode, it does. And in production, there’s no guarantee
that you’ll get the same server instance as last time, so you can’t
really rely on class variables in the controller. You might be able to
rig up something in a model…

But this smells funny. What exactly are you trying to do here? In most
cases, if data is worth saving, it’s worth putting in the DB.

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected].invalid

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