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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T00:59:38+00:00 2026-06-08T00:59:38+00:00

Given User: class User < ActiveRecord::Base has_many :discussions has_many :posts end Discussions: class Discussion

  • 0

Given

User:

class User < ActiveRecord::Base
   has_many :discussions
   has_many :posts
end

Discussions:

class Discussion < ActiveRecord::Base
    belongs_to :user
    has_many :posts
end

Posts:

class Post < ActiveRecord::Base
    belongs_to :user
    belongs_to :discussion 
end

I am currently initializing Posts in the controller via

@post = current_user.posts.build(params[:post])

My question is, how do I set/save/edit the @post model such that the relationship between the post and the discussion is also set?

  • 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-08T00:59:40+00:00Added an answer on June 8, 2026 at 12:59 am

    Save and edit discussions along with post

    Existing Discussion

    To associate the post you’re building with an existing discussion, just merge the id into the post params

    @post = current_user.posts.build(
              params[:post].merge(
                :discussion_id => existing_discussion.id
            ) 
    

    You will have to have a hidden input for discussion id in the form for @post so the association gets saved.

    New Discussion

    If you want to build a new discussion along with every post and manage its attributes via the form, use accepts_nested_attributes

    class Post < ActiveRecord::Base
      belongs_to :user
      belongs_to :discussion
      accepts_nested_attributes_for :discussion
    end
    

    You then have to build the discussion in the controller with build_discussion after you built the post

    @post.build_discussion
    

    And in your form, you can include nested fields for discussions

    form_for @post do |f|
      f.fields_for :discussion do |df|
        ...etc
    

    This will create a discussion along with the post. For more on nested attributes, watch this excellent railscast

    Better Relations

    Furthermore, you can use the :through option of the has_many association for a more consistent relational setup:

    class User < ActiveRecord::Base
      has_many :posts
      has_many :discussions, :through => :posts, :source => :discussion
    end
    
    class Discussion < ActiveRecord::Base
      has_many :posts
    end
    
    class Post < ActiveRecord::Base
      belongs_to :user
      belongs_to :discussion 
    end
    

    Like this, the relation of the user to the discussion is maintained only in the Post model, and not in two places.

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

Sidebar

Related Questions

Given the following ActiveRecord model: class User < ActiveRecord::Base has_many :games def name Joe
Models: class User < ActiveRecord::Base has_many :attendances has_many :courses, :through => :attendances end class
Given the following class User < ActiveRecord::Base has_and_belongs_to_many :companies end class Company < ActiveRecord::Base
I have a these 3 models: class User < ActiveRecord::Base has_many :permissions, :dependent =>
I have the following models, with their relevant associations: class User < ActiveRecord::Base has_many
class Users < ActiveRecord::Base has_many :meetings, :through => :meeting_participations has_many :meeting_participations end class Meetings
I have 2 models: class User < ActiveRecord::Base has_and_belongs_to_many :groups end class Group <
I have the following models: class Instance < ActiveRecord::Base has_many :users has_many :books end
My system has the following associations set up: class Book < ActiveRecord::Base has_many :
class Office < ActiveRecord::Base has_many :users searchable do text :name location :coordinates do Sunspot::Util::Coordinates.new(latitude,

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.