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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T12:42:47+00:00 2026-06-11T12:42:47+00:00

I have a simple Rails form that uses a conditional build_association (if there’s not

  • 0

I have a simple Rails form that uses a conditional build_association (if there’s not already an associated parent) to include a text_field to edit a parent. This is useful if a new record for the parent should be added. The problem with this is that if a parent already exists, if the text_field is updated then it will be changed for the parent record affecting many children. If build_association is called regardless of whether a parent association already exists, then new, duplicate parents will constantly be created.

I am trying to understand how I can use build_association and fields_for to add a new parent to the database if the user types in an unfamiliar name, but to set the parent to be an existing record if the name matches an existing parent’s name. (If I can get this part to work right, the next step will be to add autocomplete.) The code is below:

    <%= form_for @sermon, :html => { :multipart => true } do |f| %>
      <div class="field">
        <%= f.label :title %><br />
        <%= f.text_field :title %>
      </div>
      <div class="field">
        <%= f.label :date %><br />
        <%= f.text_field :date %>
      </div>
<div>
    <% @sermon.build_speaker unless @sermon.speaker %>
        <%= f.fields_for :speaker do |g| %>
            <%= g.label :name, "Speaker name:" %><br />
            <%= g.text_field :name %>
            <%= g.submit %>
        <% end %>
    </div>
      <div class="actions">
        <%= f.submit %>
      </div>
    <% end %>
  • 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-11T12:42:48+00:00Added an answer on June 11, 2026 at 12:42 pm

    I think my gem get_or_build will be useful for you. I had the same problem and didn’t find any appropriate clean solution so I’ve decided to write this gem. Enjoy!

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

Sidebar

Related Questions

I have started using jQuery and rails. I have made a simple form that
I have a simple Ruby on Rails form which includes an authenticity_token. Unfortunatly, I
I'm making a simple search form in rails. In my search view I have
Using Ruby 1.8.7-p358, Rails 3.0.12, gem responder, gem simple-form; jquery_ujs.js I have a form
I have made a simple Rails application that allows people to comment on posts.
In my Rails 3 app I have a simple form like: <%= form_tag({:controller =>
I'm new to rails and mongodb, and have a simple form attempting to create
I thought that there should have been a simple solution to this, given that
I have a nested form in a rails view that is called like this
I've got a relatively simple Rails app that takes a form with nested params

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.