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.
- Do I need a
belongs_to :postin my Discussion model? - 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?
- 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.
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.
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.