I''m building a simple forum system which features a topics and a posts table. The topic model has a has_many association with the post model, i.e.: class Topic < ActiveRecord::Base { has_many :posts } class Post < ActiveRecord:Base { belongs_to :topic, :dependent => true } Now, I wish to sort the topics by the created_at property of the last post, but I can''t for the life of me figure out how. Any help would be greatly appreciated. I''ve spent the last two days trying to solve this. -- Posted via http://www.ruby-forum.com/.
hi there, there?s a couple of ways to do that in code.. but that would involve a bit of sorting through arrays and stuff. i think the quickest way to accomplish what you want to do is to just put a last_post_created_at field into your topic model and just update that each time someone writes a new post for that topic... kind regards, alexander Alex Griffioen wrote:>I''m building a simple forum system which features a topics and a posts >table. The topic model has a has_many association with the post model, >i.e.: > >class Topic < ActiveRecord::Base { > has_many :posts >} > >class Post < ActiveRecord:Base { > belongs_to :topic, :dependent => true >} > >Now, I wish to sort the topics by the created_at property of the last >post, but I can''t for the life of me figure out how. > >Any help would be greatly appreciated. I''ve spent the last two days >trying to solve this. > > >
alexander wrote:> i think the quickest way to accomplish what you want to do is to just > put a last_post_created_at field into your topic model and just update > that each time someone writes a new post for that topic... > kind regards, > alexanderI''ve tried putting a last_post_id in the topics table, and reference it with: class Topic < ActiveRecord::Base { belongs_to :last_post, :class_name => ''Post'', :foreign_key => ''last_post_id'' } But that caused the posts counter_cache on topic.posts_count to stop working and I couldn''t quite find the right place/hook to set the last_post association :( Again, any advice is much appreciated. -- Posted via http://www.ruby-forum.com/.
oh. i?m sorry.. that?s not what i meant. my suggestion was something like this: def whateversavesyourposts code that saves your post here @topic = Topic.find(@post.topic_id) @topic.last_post_created_at(@post.created_at) @topic.save end i hope this is a bit clearer :) Alex Griffioen wrote:>alexander wrote: > > >>i think the quickest way to accomplish what you want to do is to just >>put a last_post_created_at field into your topic model and just update >>that each time someone writes a new post for that topic... >>kind regards, >>alexander >> >> > >I''ve tried putting a last_post_id in the topics table, and reference it >with: > >class Topic < ActiveRecord::Base { > belongs_to :last_post, :class_name => ''Post'', :foreign_key => >''last_post_id'' >} > >But that caused the posts counter_cache on topic.posts_count to stop >working and I couldn''t quite find the right place/hook to set the >last_post association :( > >Again, any advice is much appreciated. > > >
alexander wrote:> oh. > i?m sorry.. that?s not what i meant. > my suggestion was something like this: > > def whateversavesyourposts > code that saves your post here > @topic = Topic.find(@post.topic_id) > @topic.last_post_created_at(@post.created_at) > @topic.save > end > > i hope this is a bit clearer :)I know ;) But I thought: "Why save just the date if I could reference the entire post?" I know RForum, for instance, saves both the last_post_date and last_post_author_id... that seemed a bit nonsensical if you can just save the last post :) -- Posted via http://www.ruby-forum.com/.
Alex Griffioen <info@...> writes:> I know ;) But I thought: "Why save just the date if I could reference > the entire post?" I know RForum, for instance, saves both the > last_post_date and last_post_author_id... that seemed a bit nonsensical > if you can just save the last post :)It''s all about saving a join with the post tables. The query used to fetch the article "headers" doesn''t touch the posts table at all. -- Pazu
Pazu wrote:> It''s all about saving a join with the post tables. The query used to > fetch the > article "headers" doesn''t touch the posts table at all. > > -- PazuYou''re saying it''s smart to store last_post_created_at and last_post_author_id in the topics table, right? I still don''t see why... I''d rather use eager loading to fetch the topic''s last post, but I can''t seem to assign topic.last_post properly :( -- Posted via http://www.ruby-forum.com/.