Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6234107
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T10:25:29+00:00 2026-05-24T10:25:29+00:00

My models are like this: a discussion has_many posts (nested resource). I want to

  • 0

My models are like this: a discussion has_many posts (nested resource).

I want to add a starter_post_id column to the discussions table, and have it record the ‘thread starter post id’. The discussion is created along with the post in the nested form, and that when the logic should be called, because other posts to that discussion will be replies not starter posts.

I am not sure what I need to do after the add_column db migration.

  1. Do I need a belongs_to :post in my Discussion model?
  2. What’s the order of creation for these nested objects. e.g. parent’s creation ends before child’s starts? or will the parent constructor call the child constructor?
  3. Which model should the starter post assignment logic go to? This is related to Q2 since both objects needs to be initiated, but preferably before the DB call.
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T10:25:30+00:00Added an answer on May 24, 2026 at 10:25 am

    I tried before_save and it won’t work because at that point in time the discussion has no way to get hold of the starter post object. I was pointed out to use after_create instead.

    def after_create
        self.starter_post_id = self.posts.first.id
        self.save!
    end
    

    This will cause one extra sql query, but it is better than doing it at the post model.

    I used belongs_to so I can use discussion.start_post_id, but I guess it is optional.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two models like this: class Topic < ActiveRecord::Base has_many :articles end class
I have models like this: class IdfPracownicy(models.Model): nazwa = models.CharField(max_length=100) class IdfPracaOpinie(models.Model): nazwa =
I have two models like this: class OptionsAndFeatures(models.Model): options = models.TextField(blank=True, null=True) entertainment =
I have a two models set up like this: class User < ActiveRecord::Base #
I have methods in all of my models that look like this: def formatted_start_date
Newbie question. I have Django models that look like this: class Video(models.Model): uploaded_by =
If I have two models like this: PhotoAlbums (id, creator_id) Photos (photo_album_id, name) In
Currently my application caches models in memcache like this: memcache.set(somekey, aModel) But Nicks' post
I've got a couple django models that look like this: from django.contrib.sites.models import Site
I've got a set of models that look like this: class Page(models.Model): title =

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.