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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T04:53:57+00:00 2026-06-07T04:53:57+00:00

I was wondering what the best implementation would be to programatically generate a single

  • 0

I was wondering what the best implementation would be to programatically generate a single child object for a parent(s) without the use of a form.

In my case I have an existing forum system that I would like to tie into my Upload system via comments. I would like to manually create a child Forum object to discuss said upload in the same create action that the Upload is created. The two models have a relationship as so:

Child forum:

class Forum < ActiveRecord::Base
   ...
   belongs_to :upload
end

Parent upload:

class Upload < ActiveRecord::Base
   ...
   has_one :forum
end

I was thinking of something along the lines of:

class UploadsController < ApplicationController
  ...
  def create
    #Create the upload and a new forum + initial post to discuss the upload
    @upload = Upload.new(params[:upload])
    @forum = Forum.new(:upload_id => @upload.id, :title => @upload.name...)
    @first_post = Post.new(:forum_id => @forum.id....)    
    if @upload.save && @topic.save && @first_post.save
      redirect_to :action => "show", :id => @upload.id
    else
      redirect_to :action => "new"
    end 
  end

end

Which is fairly close to what I wanted to do but the parent ids aren’t generated until the parent objects are saved. I could probably do something like:

@upload.save 
@forum = Forum.new(:upload_id => @upload.id...
@forum.save....

But I thought it might be cleaner to only persist the objects if they all validated. I’m not sure, does anybody else know of a better implementation?

  • 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-07T04:54:00+00:00Added an answer on June 7, 2026 at 4:54 am

    I would recommend moving the forum creation from the controller to the model. The forum will only be created on the successful creation of the Upload.

    class Upload < ActiveRecord::Base
      ...
      has_one :forum
      after_create :create_forum
      ...
      def create_forum
        Forum.create(:upload_id => self.id, :title => self.name...)
      end
    end
    
    
    class Forum < ActiveRecord::Base
      ...
      has_many :posts
      after_create :create_first_post
      ...
      def create_first_post
        Post.new(:forum_id => self.id)
        # or self.posts << Post.new()
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Wondering what the best / good way of doing this would be in jQuery.
Just wondering what the best way to access read only information form a Microsoft
Just wondering what the best practise advice would be on the architecture for a
I was wondering the best implementation of a DAG (Directed Acyclic Graph) using Scala/Akka
So, in a single parent inheritance model what's the best solution for making code
Just wondering the best way to replace in place matches on a string. value.replace(bob,
I'm wondering about best practice here. Is it good practice for a factory method
I was wondering the best way to upload file to a web server in
I'm new to web development and am just wondering about best practices for java
I am writing a simple multithreaded socketserver and I am wondering how best to

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.