Forum: Ruby on Rails Avoiding repeated calls to the DB

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Robert H. (Guest)
on 2007-01-25 21:47
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?
Mark D. (Guest)
on 2007-01-25 21:53
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.
Mark D. (Guest)
on 2007-01-25 21:54
> This will only load once per model I think - but I am not 100% certain.

I meant 'once per instance'...
Robert H. (Guest)
on 2007-01-25 22:38
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?
Robert H. (Guest)
on 2007-01-25 23:39
Robert H. wrote:
>
> Wouldn't the condition have to include thread_id?

Or is that implied in has_one?
Mark D. (Guest)
on 2007-01-25 23:59
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.
Robert H. (Guest)
on 2007-01-26 00:45
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?
Rick O. (Guest)
on 2007-01-26 04:54
(Received via mailing list)
> 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
http://errtheblog.com/post/27

cached_model
http://dev.robotcoop.com/Libraries/

--
Rick O.
http://weblog.techno-weenie.net
http://mephistoblog.com
This topic is locked and can not be replied to.