# What am I missing? Activerecord & Sessions!

**URL:** <https://www.ruby-forum.com/t/what-am-i-missing-activerecord-sessions/75721>\
**Category:** Rails\
**Created:** [March 2, 2006, 4:47pm UTC](https://www.ruby-forum.com/t/what-am-i-missing-activerecord-sessions/75721 "2006-03-02T16:47:56Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tyler\_Keating](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/df705f/32.png) [@Tyler\_Keating](https://www.ruby-forum.com/u/Tyler_Keating)\
**Post date:** [March 2, 2006, 4:47pm UTC](https://www.ruby-forum.com/t/what-am-i-missing-activerecord-sessions/75721/1 "2006-03-02T16:47:56Z")

</div>

Can someone please explain why every time that I call an action in my  
controller, every model that is stored in the session needs to be  
interpreted by a database query? If I have four models stored in the  
session, I end up with five long “SELECT a.attname,  
format\_type(a.atttypid, a.atttypmod), d.adsrc, a.attnotnull FROM  
pg\_attribute a LEFT JOIN pg\_attrdef d…” queries when _ANY_ action is  
called. I’ve tried adding the models in my ApplicationController, but  
to no avail.

It is extremely annoying to sort through the console messages with these  
repeating queries. Is this supposed to be happening??? Why do normal  
ActiveRecord objects not need this query when they are used??

It started when I moved to PostgreSQL.

Thanks for any insight,  
Tyler

---

<div class="post-metadata">

**Author:** ![Tyler\_Keating](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/df705f/32.png) [@Tyler\_Keating](https://www.ruby-forum.com/u/Tyler_Keating)\
**Post date:** [March 3, 2006, 4:12pm UTC](https://www.ruby-forum.com/t/what-am-i-missing-activerecord-sessions/75721/2 "2006-03-03T16:12:06Z")

</div>

Well, I figured out that the metadata queries are normal behaviour,  
because in development mode I’m not caching classes (ie.  
config.cache\_classes = false). To not see the queries, I simply need to  
cache the classes so that the metadata is only queried once and then  
saved.

I don’t want to have to keep restarting the server repeatedly in between  
each change, but the metadata queries are becoming unbearable. Consider  
loading a list of images from the database using an action. With 4  
models in the session plus the image model, I end up with 35 lines of  
log garbage for each image retrieved. Multiply that by 10 images and  
it’s a total mess.

My question again “Is there a way to hide the metadata queries and still  
not cache classes???” I’m considering going back to MySQL just because  
the metadata queries are at least compact “SHOW FIELDS FROM xxxx;”

Tyler

---

<div class="post-metadata">

**Author:** ![Tyler\_Keating](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/df705f/32.png) [@Tyler\_Keating](https://www.ruby-forum.com/u/Tyler_Keating)\
**Post date:** [March 3, 2006, 5:11pm UTC](https://www.ruby-forum.com/t/what-am-i-missing-activerecord-sessions/75721/3 "2006-03-03T17:11:28Z")

</div>

Is it important to have the actual model objects in the session? I find  
it’s usually sufficient to keep the model id in the session, and avoids  
just this sort of problem.

–josh  
[http://blog.hasmanythrough.com](http://blog.hasmanythrough.com)

Tyler Keating wrote:

> Well, I figured out that the metadata queries are normal behaviour,  
> because in development mode I’m not caching classes (ie.  
> config.cache\_classes = false). To not see the queries, I simply need to  
> cache the classes so that the metadata is only queried once and then  
> saved.
> 
> I don’t want to have to keep restarting the server repeatedly in between  
> each change, but the metadata queries are becoming unbearable. Consider  
> loading a list of images from the database using an action. With 4  
> models in the session plus the image model, I end up with 35 lines of  
> log garbage for each image retrieved. Multiply that by 10 images and  
> it’s a total mess.
> 
> My question again “Is there a way to hide the metadata queries and still  
> not cache classes???” I’m considering going back to MySQL just because  
> the metadata queries are at least compact “SHOW FIELDS FROM xxxx;”
> 
> Tyler

---

<div class="post-metadata">

**Author:** ![Tyler\_Keating](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/df705f/32.png) [@Tyler\_Keating](https://www.ruby-forum.com/u/Tyler_Keating)\
**Post date:** [March 3, 2006, 6:03pm UTC](https://www.ruby-forum.com/t/what-am-i-missing-activerecord-sessions/75721/4 "2006-03-03T18:03:09Z")

</div>

I have gone through my session use and reduced the number of model  
objects. I would say at the expense of more database queries, but I  
realize that I’ll be storing the session objects in the database at some  
point down the road anyways. It’s an improvement for sure, but still  
doesn’t solve displaying several database images on a page, because each  
action must query the metadata for the Image class. Just trying to  
display ten images creates 60 extra lines in the log everytime I reload  
a page.

I guess I’ll have to implement image caching sooner than later…  
Thanks,  
Tyler

Josh S. wrote:

> Is it important to have the actual model objects in the session? I find  
> it’s usually sufficient to keep the model id in the session, and avoids  
> just this sort of problem.
> 
> –josh  
> [http://blog.hasmanythrough.com](http://blog.hasmanythrough.com)
> 
> Tyler Keating wrote:
> 
> > Well, I figured out that the metadata queries are normal behaviour,  
> > because in development mode I’m not caching classes (ie.  
> > config.cache\_classes = false). To not see the queries, I simply need to  
> > cache the classes so that the metadata is only queried once and then  
> > saved.
> > 
> > I don’t want to have to keep restarting the server repeatedly in between  
> > each change, but the metadata queries are becoming unbearable. Consider  
> > loading a list of images from the database using an action. With 4  
> > models in the session plus the image model, I end up with 35 lines of  
> > log garbage for each image retrieved. Multiply that by 10 images and  
> > it’s a total mess.
> > 
> > My question again “Is there a way to hide the metadata queries and still  
> > not cache classes???” I’m considering going back to MySQL just because  
> > the metadata queries are at least compact “SHOW FIELDS FROM xxxx;”
> > 
> > Tyler
