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

  • SEARCH
  • Home
  • 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 8178523
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T23:47:14+00:00 2026-06-06T23:47:14+00:00

I am creating a forum software. I want to implement closing topic functionality. Closing

  • 0

I am creating a forum software. I want to implement closing topic functionality. Closing a topic will prevent anyone from creating posts for the closed topic.

My current implementation

  • TOPIC has many posts
  • When a TOPIC is created, by default, a POST is also created for that TOPIC. Basically TOPIC has a title and derives body of content from POST’s content.
class AddClosedToTopic < ActiveRecord::Migration
  def change
    add_column :topics, :closed, :boolean, default: false
  end
end
  • TOPIC has a column called closed. If closed is true, the topic is closed. By default, it’s set to false.
  • Whenever a user tries to create a post, I use the validation below to check if the topic is closed or not.
class Post < ActiveRecord::Base
  . . .
  validate :cannot_create_replies_to_closed_topic

  # custom validation methods
  def cannot_create_replies_to_closed_topic
    topic = Topic.find_by_id(topic_id)
    if topic.closed?
      errors.add(:content, "can't be created for a topic that is closed")
    end
  end
end

Problem

This way works perfectly fine for a TOPIC that’s becomes closed some time after it’s created. However, when I create a new TOPIC, it becomes problematic.

Since in the POST validation, I do topic = Topic.find_by_id(topic_id), new topic that is currently being created won’t be in the database. So it throws NoMethodError.

NoMethodError in TopicsController#create
undefined method `closed?' for nil:NilClass

app/models/post.rb:16:in `cannot_create_replies_to_closed_topic'
app/controllers/topics_controller.rb:21:in `create'

Possible solution?

I think I have some method that can refer to the current topic being created somehow in the POST validation, I think this problem can be solved.

How can I do this? Or do you guys have other methods to solve this problem?

  • 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-06-06T23:47:15+00:00Added an answer on June 6, 2026 at 11:47 pm

    You could try something like an after_create callback in your Topic model to only create the post AFTER your topic is created, and set the topic_id in that method.

    Something like this:

     after_create :create_initial_post
    
     def create_initial_post
       post = Post.new(:topic_id => self.id)
       post.save
     end
    

    This should solve your problem. You may have to modify some of your code to ensure that it creates the topic first, and then it should create the initial post after that. You may also want to modify your cannot_create_replies_to_closed_topic method to ensure the initial post can be created.

    Hope this points you in the right direction!

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

Sidebar

Related Questions

I am creating a forum software using php and mysql backend, and want to
I am creating a forum software. I want admins and mods to be able
I'm creating custom forum software for a site I'm building, which includes 2 tables
I am creating some tag functionality for a forum using linq2sql, and I have
I want to automate the process of creating a forum category with the phpBB
I want to use a good mvc naming convention for a forum im creating.
hi friends I am creating a software in java swing where i want to
I'm creating a forum, so I have created a table with posts. One of
I am creating a forum for my school. A student will be able to
Basically i am creating a vbnet system software. I want to create a shortcut

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.