Has_one frustrations, 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:


t :topics
c :id
c :last_post_id

t :posts
c :id
c :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:


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

Next, in the same model, I have the following function which I call
whenever a new post is added to a topic:


def set_last_post
self.last_post = self.posts.last
end

Posts are added properly to the topics (@topic.posts << @post;
@topic.save), yet somehow, every topic I add has NULL for the
last_post_id. Please help, I’m losing my mind.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs