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 8172223
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T21:42:50+00:00 2026-06-06T21:42:50+00:00

I am wondering how the new and create method of TOPIC should look like,

  • 0

I am wondering how the new and create method of TOPIC should look like, if TOPIC belongs to USER and FORUM.

class Forum < ActiveRecord::Base
  belongs_to :user
  has_many :topics, :dependent => :destroy
end

class User < ActiveRecord::Base
  has_many :forum
  has_many :topic
end

class Topic < ActiveRecord::Base
  belongs_to :user
  belongs_to :forum
end

When TOPIC belonged to only FORUM, I made use of build method like this.

def new
  @forum = Forum.find(params[:forum_id])
  @topic = @forum.topics.build
end

def create
  @forum = Forum.find(params[:forum_id])
  @topic = @forum.topics.build(params[:topic])
  if @topic.save
    flash[:success] = "Success!"
    redirect_to topic_posts_path(@topic)
  else
    render 'new'
  end
end

But now that TOPIC belongs to both FORUM and USER, I am not sure what to do? Any suggestions on this topic?

Here’s my schema for reference. Ignore POST for now.

create_table "forums", :force => true do |t|
  t.datetime "created_at",  :null => false
  t.datetime "updated_at",  :null => false
  t.string   "name"
  t.text     "description"
  t.integer  "user_id"
end

create_table "posts", :force => true do |t|
  t.datetime "created_at", :null => false
  t.datetime "updated_at", :null => false
  t.integer  "topic_id"
  t.text     "content"
  t.integer  "user_id"
end

create_table "topics", :force => true do |t|
  t.datetime "created_at",   :null => false
  t.datetime "updated_at",   :null => false
  t.integer  "forum_id"
  t.string   "name"
  t.integer  "last_post_id"
  t.integer  "views"
  t.integer  "user_id"
end

create_table "users", :force => true do |t|
  t.string   "name"
  t.string   "email"
  t.datetime "created_at",                         :null => false
  t.datetime "updated_at",                         :null => false
  t.string   "password_digest"
  t.string   "remember_token"
  t.boolean  "admin",           :default => false
end

My solution to new create method

def create
  @forum = Forum.find(params[:forum_id])
  @topic = @forum.topics.build(params[:topic])
  @topic.user_id = current_user.id

  if @topic.save
    flash[:success] = "Success!"
    redirect_to topic_posts_path(@topic)
  else
    render 'new'
  end
end

current_user returns current user object.

  • 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-06T21:42:53+00:00Added an answer on June 6, 2026 at 9:42 pm
    topic = Topic.new
    topic.forum = Forum.first
    topic.user = User.first
    
    topic = Topic.create(:forum => Forum.first, :user => User.first)
    
    topic = @user.topics.build(params[:topic])
    topic.forum = @forum
    
    etc
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm facing a requirement to create a static method on my base class, but
Just wondering if anyone has some good resources for learning how to create new
I am wondering if it is possible to create a new dataframe with certain
New to Subsonic 3.0 and wondering how to perform LIKE operators on object attributes.
In several pieces of sample objective-c code I've seen people create new objects like
I have this as my create method inside my PostsController class PostsController < ApplicationController
Just wondering, is it necessary to programmatically create a new thread in a situation
In PHP if I create a singleton method for like 5 different caching classes
I was wondering if it is safe to create new objects as part of
I am wondering as a new Android developer (10+ years C# OOP) which would

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.