SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Hybrid View

  1. #1
    SitePoint Member
    Join Date
    Jul 2006
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Post how to get data from the database only once

    Hi,
    I am a little bit new to ruby. I have a requirement of getting the data from the database, many times in an application. Is there a way that I can get the data only once from the database and use it whereever I want? ( I am trying not to use the global variable in this process.) If I can use a singleton class, how can I use it in ruby

    Thanks in Advance.

    Regards,
    Anand.
    Last edited by sanand_83; Jul 19, 2006 at 03:52.

  2. #2
    SitePoint Addict
    Join Date
    Mar 2005
    Posts
    251
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm not sure exactly what you want to do here, maybe you could expand upon 'the data'. are you referring to one row of data or the whole table?

    Ruby uses active record to access database records, one active record instance represents one row of the database.

    So row = User.findfirst) will assign the first row of the User table to the variable row.

    If it's one particular row you are after you can set a method in the model to return it, otherwise if you don't know what row you'll be fetching it's much better to only fetch the row when you need it.

    I'm not sure if I've understood your question though, so post back if you can give us some more info....

  3. #3
    SitePoint Member
    Join Date
    Jul 2006
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.findall). 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 application scope.

    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
    http://www.oreillynet.com/ruby/blog/...singleton.html

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

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

  4. #4
    SitePoint Zealot
    Join Date
    Jul 2004
    Location
    Oklahoma
    Posts
    119
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by sanand_83
    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.findall). 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).
    Because each request in a production Rails app will effectively reload the Ruby interpreter (just like in PHP) a global variable will be erased on the next request. You could put the states into the session, but depending on your application configuration, the session may be in the database as well. And unless you can provide a reason for your statement about hackers, I'll consider it FUD.
    Its something like I need the information of the states in the application scope.

    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.
    The Singleton is an object creation pattern, (which in Java I'm sure has been abstracted to a usable class). It is typically used as an object oriented approach to global variables.
    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....
    Put it in the session. But this is going to be a per-user approach, not per 'application scope' (Which by the way has no meaning in a Rails environment).
    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
    http://www.oreillynet.com/ruby/blog/...singleton.html

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

    Thanks for making me clear what I wanted....
    Again, a Singleton is just an object following a specific pattern. I highly suggest you get a book on Rails, to get a better grasp on the request cycle, 'application scoep' and related issues.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •