The SitePoint Forums have moved.

You can now find them here.
This forum is now closed to new posts, but you can browse existing content.
You can find out more information about the move and how to open a new account (if necessary) here.
If you get stuck you can get support by emailing forums@sitepoint.com

If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Losing mind over has_one association, please help

Hi everyone. I've been bending my head around a has_one association for about 3 hours now and I can't get it to work. I'm pretty sure it's not something I'm overlooking anymore. Here's the essential part of the schema I'm working with:

Code:

topics(id, last_post_id)
posts(id, topic_id)

Topics are simply named containers for posts. I decided not to use virtual tables since 99% of the columns would be topic-related. In the Topic model I have the following associations:

Code:

has_many(:posts)
has_one(:last_post, {:class_name => 'Post'})

Next, in the Post model, I have the following hook which should update the topic whenever a new post is added:

@ Fenrir2: I just realized that if I have the foreign ID's (first_post and last_post) stored in the table, I could sort by them as a newer post will always have a higher ID and vice versa Thanks anyway!

@ adamp: I tried that, but were you able to sort the topic list by, say, topic title, last post date or first post date using that association?

I just wrote a basic forum in order to first get to grips with Rails, so didn't take it very far, but if you need to sort the post list by various fields you should be able to replace the 'dte_created ASC' part of the rder call with a call to a function that checks the @params for the field and direction to sort on.