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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T11:24:12+00:00 2026-05-23T11:24:12+00:00

My app has three models: Thread (has_many :thread_apps) ThreadApp (belongs_to :thread, has_many :forms, :as

  • 0

My app has three models:

Thread (has_many :thread_apps)
ThreadApp (belongs_to :thread, has_many :forms, :as => :appable)
Form (belongs_to :app)

ThreadApp Fields: thread_id, form_id, appable_id, appable_type

What I want to be able to do is when creating a form, ensure that a ThreadApp record is also created to make the association:

Here is what I have:

class FormsController < ApplicationController

 def create
    @thread = Thread.find(params[:thread_id])
    @thread_app = @thread.thread_apps.new
    @form = @thread_app.forms.build(params[:form].merge(:user_id => current_user.id))
    @form.save
    ....

This is saving the form nicely, but the thread_app associated is not being made? Any ideas why?

Thank you

  • 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-23T11:24:13+00:00Added an answer on May 23, 2026 at 11:24 am

    callings model.save does not save associations unless you tell it to

    you can set autosave

    class Form < ActiveRecord::Base
       belongs_to :thread_app , :autosave => true
    end
    

    or call save on it in the controller

    @thread_app.save
    

    or you can take it out of the controller entirely
    and do this with a callback

    class Form < ActiveRecord::Base
       before_create :create_thread_app
       def create_thread_app
         self.thread_app ||= ThreadApp.create(...)
       end
    end
    

    or after_create, _before_validation_on_create, or any other call back would work

    —UPDATE—

    this might make a difference using create inse=tead of new, and appables that you specified as ‘as’

    class FormsController < ApplicationController
    
     def create
        @thread = Thread.find(params[:thread_id])
        @thread_app = @thread.thread_apps.create
        @form = @thread_app.appables.build(params[:form].merge(:user_id => current_user.id))
        @form.save
        ....
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My app has three text fields: 1) Username // Initialization code usernameTextField = [[UITextField
Let's say my app has two models, Foo and Bar. Foo optionally belongs_to Bar.
I'm writing a WinForms app which has two modes: console or GUI. Three projects
I'm building C#/.Net 3.5 app using three layers: UI (view/view models), service, and data
I have a simple rails app with models project and phase. A project has
I have three Django models that exist in a hierarchy: Protocol -> has Test
I have a rails app which currently has 3 models: Post , User and
I have three models. Call them A, B, and C. Model A has _post_put_hook
I've got a simple Django photography competition app that has three simple model definitions:
In my Rails 3 project I have three models: Projects has_many Feeds, and Feeds

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.