Avoiding repeated calls to the DB

Let’s say I’ve got a forum app where there are Topic objects and Post
objects.

Each Post has a topic_id column and a parent_id column.

Each topic has a root_post that, minus error-checking, etc., boils down
to:

def root_post
Post.find(
:first,
:conditions => “topic_id = #{self.id} and parent_id IS NULL”
)
end

Where can I store the root_post so that I don’t have to go to the DB
every time I need it?

This just has to be brain dead simple, but I haven’t found the answer
yet.

If I put any ActiveRecord into the session, I get marshalling problems
when the session is saved to the DB. Perhaps there is a place to store
things in the session that don’t get saved? Or a more general
app-level caching mechanism?

This won’t really solve your problem, but you could simplify your
root_post method as a ‘has_one’ association with an order specified:

has_one :root_post, :class_name => ‘Post’, :order => ‘posted_at DESC’,
:conditions => ‘parent_id IS NULL’

This will only load once per model I think - but I am not 100% certain.

This will only load once per model I think - but I am not 100% certain.

I meant ‘once per instance’…

Mark D. wrote:

This won’t really solve your problem, but you could simplify your
root_post method as a ‘has_one’ association with an order specified:

has_one :root_post, :class_name => ‘Post’, :order => ‘posted_at DESC’,
:conditions => ‘parent_id IS NULL’

This will only load once per model I think - but I am not 100% certain.

Wouldn’t the condition have to include thread_id?

I’m not sure how the Thread model fits into your system. Assuming that
you have two models:, you can simply do:

class Topic < ActiveRecord::Base

has_many :posts
has_one :root_post, :class_name => ‘Post’, :conditions => ‘parent_id
IS NULL’
has_many :replies, :class_name => ‘Post’, :offset => 2

end

class Post < ActiveRecord::Base

belongs_to :topic

end

All that is needed to link these up is a ‘topic_id’ fk in your post
table.

~ Mark D.

Mark D. wrote:

I’m not sure how the Thread model fits into your system. Assuming that
you have two models:, you can simply do:

class Topic < ActiveRecord::Base

has_many :posts
has_one :root_post, :class_name => ‘Post’, :conditions => ‘parent_id
IS NULL’
has_many :replies, :class_name => ‘Post’, :offset => 2

end

class Post < ActiveRecord::Base

belongs_to :topic

end

All that is needed to link these up is a ‘topic_id’ fk in your post
table.

~ Mark D.

Thanks.

By ‘Thread’, I meant ‘Topic’.

I’m still curious about the more general case. How do I store
ActiveRecords in the Session without marshalling problems?

Robert H. wrote:

Wouldn’t the condition have to include thread_id?

Or is that implied in has_one?

If I put any ActiveRecord into the session, I get marshalling problems
when the session is saved to the DB. Perhaps there is a place to store
things in the session that don’t get saved? Or a more general
app-level caching mechanism?

The only built-in caching deals with caching the rendered output.
Folks tend to use something like memcache to cache the actual models
though. Look at acts_as_cached and cached_model:

acts_as_cached

cached_model


Rick O.
http://weblog.techno-weenie.net
http://mephistoblog.com