On Mar 12, 12:48 am, “Ryan B. (Radar)” [email protected]
wrote:
s.ross, that will find the latest topic, not necessarily the the latest
post. For example, I create two topics, Topic A and Topic B. It says that
Topic B is the newest topic. I post in Topic A, and using your code Topic B
will still display as the latest topic.
if your database supports subselects (like PostgreSQL), you could do a
find_by_sql with sql like this:
SELECT DISTINCT t.*, last_post
FROM topics t
JOIN (SELECT topic_id, MAX(created_at) FROM posts GROUP BY topic_id)
as foo
ON (foo.topic_id = t.id)
ORDER BY last_post DESC
This will involve a full table scan on posts and topics, but should
work well until you have a very large database. It will always be
faster than doing the equivalent processing in ruby/rails using
ActiveRecord objects.
However, when your forum became very large and successful, you’d have
to optimize this by storing last_post_id, updating when posts are
created, and set up
belongs_to :last_post, :class_name => ‘Post’, :foreign_key
=> :last_post_id
then in a single
Topic.find(:all, :include => [:last_post], :order => “post.created_at
DESC”)
you could load the Topics, in descending order by last post, with the
last post already instantiated so you could show those details in your
listing, and it would be easy to add the appropriate limit and offset
parameters for paging. That’s what I would do, if I was writing forum
software…
Jim Crate
Que Viva, LLC