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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T12:21:41+00:00 2026-05-23T12:21:41+00:00

I have a model Post which belongs_to one Section . There are two different

  • 0

I have a model Post which belongs_to one Section. There are two different Section subclasses and I use STI to implement different behavior for each of them. In the Post form I would like to have a tab for each Section. The tab will let the user either A) Pick from an existing Section using a <select> or B) Let the user create a new Section. I would like to know how to use accepts_nested_attributes_for and fields_for or whatever is required to get this done The Rails Way.

Any advice is greatly appreciated. Thanks.

  • 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-23T12:21:42+00:00Added an answer on May 23, 2026 at 12:21 pm

    Assuming the tabs correspond to the two subclasses

    class Post
      # the two subclasses. Each instance will only be using one or the other
      belongs_to :section_foo
      belongs_to :section_bar
    
      accepts_nested_attributes_for :section_foo
      accepts_nested_attributes_for :section_bar
    end
    

    And in the view (probably once per tab)

    = form_for @post do |f|
      = f.select :section_id, SectionFoo.all # etc
      = fields_for @post.build_section_foo do |s|
        = s.text_field :bla_bla_bla
    

    That should get you 85% of the way there. You might need some :reject_if bidness on the accepts_* to avoid creating a new section and assigning an old section.

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

Sidebar

Related Questions

I have two models, Article and Post that both inherit from a base model
I have a model that has counter_cache enabled for an association: class Post belongs_to
Suppose I have a Post model which has_many Comments. I want to get the
i have this in my BlogRepository public IQueryable<Subnus.MVC.Data.Model.Post> GetPosts() { var query = from
I have a model Question with a field called userid , before one ask
I have a model Foo which have a ForeignKey to the User model. Later,
I have a model called NoteCategory, which is functioning as a join table between
I have a message form which uses validates_length_of in the model to limit the
I have a static method 'findAll' on a model which basically gets all rows
Im using Rails 2.3.5 and I have a model, let's call it Post. I

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.