Getting data from the database only once

Hi,
I am a little bit new to ruby. I have a requirement that I need to
get the data from the database. I need to do this many times in the
whole application. But, I am looking for a way with which I can get the
data from the database table only once and store it in some variable,
and I can use it anywhere I want.(I am trying not to use the global
variable for this purpose). Is there a way for this. Please respond a
lttle bit quick.

Thanks in advance.

Regards,
Anand

Anand S. wrote:

Hi,
I am a little bit new to ruby. I have a requirement that I need to
get the data from the database. I need to do this many times in the
whole application. But, I am looking for a way with which I can get the
data from the database table only once and store it in some variable,
and I can use it anywhere I want.(I am trying not to use the global
variable for this purpose). Is there a way for this. Please respond a
lttle bit quick.

Thanks in advance.

Regards,
Anand

Hi Anand,

Using the .find or .find_by_sql commands will pull data from your
database and put it into an array. Having said that, they can all be
accessed in your controller quite easily.

I hope I answered your question.

thanks,

Bing

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
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

Click
here…

Hope I am Clear ???^%&(

Thanks for making me clear what I wanted…

Anand,

Try acts_as_enumerated - it will load at application startup and utilize
throughout w/o having to fetch again.

http://wiki.rubyonrails.com/rails/pages/Acts+As+Enumerated+Plugin

I use this for several lookup values - like your state situation. You
can use one table and add virtuals to it so that you have a master
lookup table with name/value pairs.

Let me know if you need additional details about it.

Regards,

Michael

hi,

i wonder why u trying to fetch states from database…
states dont change regularly you can use javascript instead (just a
thought…u are to implement)

reagrds
gaurav

On 7/25/06, Anand S. [email protected] wrote:

Hope I am Clear ???^%&(

Thanks for making me clear what I wanted…


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


Rails mailing list
[email protected]
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.

On 7/25/06, Ezra Z. [email protected] wrote:

states to display in a combo box in a rhtml page.

Anand-
http://scottstuff.net/presentations/rails-caching/

Cheers-
-Ezra


Rails mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails

Ezra,

Would this cause issues if say, different options need to be selected
based
on object values or different defaults for different views?

Wouldn’t a fragment cached version be permenantly selected on one option
by
default?

On Jul 24, 2006, at 10:36 PM, Anand S. wrote:

all the states and display in the rhtml page.

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


Rails mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails

Anand-

I think what you want is fragment caching. This way you could do the

query once and rails can store the results on disk so next time you
need it it will be served from the cache and not hit the db. Here are
a few links to get you started.

http://rails.rubyonrails.com/classes/ActionController/Caching/
Fragments.html
http://scottstuff.net/presentations/rails-caching/

Cheers-
-Ezra

On Jul 24, 2006, at 11:04 PM, Daniel N wrote:

Would this cause issues if say, different options need to be
selected based on object values or different defaults for different
views?

Wouldn’t a fragment cached version be permenantly selected on one
option by default?

Now I’m confused. I was so impressed by a trick someone mentioned
this week for fragment caching separately by user that I saved the
message. But the message was from you!

<%= all your cached frag -%>
<% end %>

This will scope the fragment to those parameters, and you can re-
use it elsewhere if you want. Just include the same url in the cache.

Be sure to expire the fragment if the data changes. expire_fragment
( same_url_hash_used_in_cache_call )

    - dan

Yes I did send that… Sorry if this is confusing.

What I’m specifically refering to here, is that if a select box, all
by itself is fragmet cached, then that select box is set, default
value and everything. (at least I think it is, that’s what I was
asking Ezra)

Form elements are not really good things to cache in my exp, because
you cannot easily update their values on the server side. (Excepting
RJS)

Take this fragment

<%= text_field “user”, “name” %>

If this is cached, then on a new form, all well and good, but if that
form is submitted and returns errors, then the frag cache will blank
it out, ie return the original frag, a blank text field.

Likewise if just that field were cached and the same form partial were
used for an edit action, the field would be blank as well even if
normally it would have something in it.

Also, if the first time the frag is generated it contains data (the
edit action), then that data will become the default value until the
frag is expired and regenerated.

At least this is my understanding of how it works. That’s why in your
situation (other post) it’s good to tie it to a user. Each user will
have their own frag generated and stored seperately when a :user =>
@user.id is included in the cache call.

Things like slabs of non-form markup are good for cacheing. ie

<%= @user.name -%>
<%= @user.street -%>
<%= @user.city -%>

Could all be wrapped up in a frag cache quite nicley. As I said in my
other post, make sure to expire it if you change the data.

Also none of these effects will be noticed in development by default.
Caching is turned off in development.

Hi~

On Jul 25, 2006, at 12:31 AM, Daniel N wrote:

Form elements are not really good things to cache in my exp, because

have their own frag generated and stored seperately when a :user =>

but it can be anything. You’re not restricted to real things
This will scope the fragment to those parameters, and you can re-
http://www.dankohn.com/ tel:+1-415-233-1000
view processing of
processing is done on the content, it’s just inserted into the output.

For this reason, it’s not good to cache forms or form fields, since
they generally need processing each time they are displayed,
setting values, highlighting errors etc.

I hope this makes sense.

You are right Dan, I didn't think about the value of the form field.

You could get around it by using javascript onload to set the correct
value for the select but now we are getting way too complicated :wink:

So you have a few other options i guess. You could put it as a

constant in environment.rb that does the lookup upon startup and
stores it for use anywhere in your app. Now if you do this, keep in
mind that you will get one database query for this data fro meach
mongrel or fcgi proc you run but then they won’t have to hit the db
after this for the data. I had forgotten about the acts_as_enumerated
plugin and I think that is a good option too. Or you could always
write a simple yaml file type of rig. Where the first time your app
demands the code you want to cache you do the query and also write
the yaml file out to disk. You will hide the way this works in a
method in your model so that when its called it will check for the
yaml file on disk and only if its not there will it do the query and
write it out again. Something like this:

class MyModel < AR::Base

def self.select_contents
return @select_data if @select_data
path = “#{RAILS_ROOT}/tmp/select_contents.yml”
if File.exist? path
@select_data = File.open(path) { |f| YAML::load( f ) }
else
@select_data = find(:all) # or whatever the query is
File.open(path, ‘w’) { |f| YAML::dump(@select_data, f ) }
@select_data
end
end

end

And then if you need to regenerate the select_data you can just

delete the “#{RAILS_ROOT}/tmp/select_contents.yml” file.

Cheers-
-Ezra

On 7/25/06, Daniel N [email protected] wrote:

RJS)
used for an edit action, the field would be blank as well even if

Caching is turned off in development.

an eg might be
( same_url_hash_used_in_cache_call )

    - dan


Dan K. mailto:[email protected]
http://www.dankohn.com/ tel:+1-415-233-1000

Looking back at this post it’s potentially more confusing than what it
tries
to answer :frowning:

I’ll try again.

my understanding of fragment cacheing is that it has a flow in the view
processing of

When the view comes to a cache( parameters_hash ) in the view

  • if a frag, uniquely identified by parameters_hash is already generated
    – insert pre-generated frag without processing cache block
    -else
    – process cache block and save the results as a frag cache
    -end

If a cache exists that is identified by parameters_hash then no
processing
is done on the content, it’s just inserted into the output.

For this reason, it’s not good to cache forms or form fields, since they
generally need processing each time they are displayed, setting values,
highlighting errors etc.

I hope this makes sense.

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