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

Daniel N
Tue, 25 Jul 2006 05:45:14 -0000

On 7/25/06, Anand Srinu <anand.srinivasan@compassites.net>
wrote:
>
> 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/.
> _______________________________________________
> Rails mailing list
> Rails@lists.rubyonrails.org
> http://lists.rubyonrails.org/mailman/listinfo/rails



Perhaps store this in a constant that is included at the end of the
environment file

VALID_STATES = State.find(:all)

This VALID_STATES is then available everywhere in your app but is distinct
to one instance of it.  ie. If your running a mongrel cluster each mongrel
instance will have it''s own copy.

I don''t really know if this is any better than having a global variable
tho,
basically the same thing, and it certainly won''t be much good if you
want to
add/edit/remove a state.
-------------- next part --------------
An HTML attachment was scrubbed...