[Rails] Re: Getting data from the database only once

Anand Srinu
Tue, 25 Jul 2006 05:37:05 -0000

I think I will rephrase my question.

I have an application.
I have one table, states(which holds the details regarding the different
states) . In one controller(say sign_up_controller.rb), I need all the
states to display in a combo box in a rhtml page.

for getting all the states I use  get_states = States.find(:all). I get
all the states and display in the rhtml page.

Now in another controller (say friends_details_controller.rb), I again
require all the states. This time If I wanna get the states, I need to
quiery again to the SQl. right??

I dont want to do that....

I can use another option of getting all the states in the
application.rb, store it in a global variable ($states) and use it
anywhere. But globalvariables are better not be used in real time
applications, as it may help the hackers(I dont know in what way).

Its something like I need the information of the states in the
[B]application scope[/B].

In java, there is a class called Singleton , which allows to create only
one object for that class. So u can create the object, store the
information in that and use it through out the application.

I wanna know if there is a way by which I can get all the details of the
states table(all the rowsand alll the columns), get it only once, put it
in the application scope and use it anywhere without quieying again....

I did some searching on the singleton  class in ruby. I got the code to
create a singleton class. I don''t know how to use this for my purpose.

I URL I got is

<a
href="http://www.oreillynet.com/ruby/blog/2006/01/design_patterns_singleton.html">Click
here....

Hope I am Clear   ????^%&(

Thanks for making me clear what I wanted....

--
Posted via http://www.ruby-forum.com/.