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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T20:52:25+00:00 2026-06-02T20:52:25+00:00

I’m trying to make a form to set values to two children objects through

  • 0

I’m trying to make a form to set values to two children objects through has_one (with :class_name option) and belongs_to relation. However, when I input and submit values through the form, both children objects have the same value even if I input different values.

I have the these two models.
(two children objects above indicates “origin” and “destination” whose class names are “Place”)

class Route < ActiveRecord::Base
  attr_accessible :name, :destination_attributes, :origin_attributes                                            
  has_one :origin, :class_name=>"Place"
  has_one :destination, :class_name=>"Place"
  accepts_nested_attributes_for :origin, :destination
end

class Place < ActiveRecord::Base                                                                                                                                              
  attr_accessible :address, :lat, :lng, :name, :route_id
  belongs_to :route, :foreign_key => "route_id"
end

And made form using partial like following.

routes/_form.html.erb

<%= form_for(@route) do |f| %>
  <div class="field">
    <%= f.label :name %><br />
    <%= f.text_field :name %>
    <br />
    <%= render :partial => "places/nested_places_form", :locals => {record_name: :origin, place_object: @route.origin, parent_form: f} %>
    <br />
    <%= render :partial => "places/nested_places_form", :locals => {record_name: :destination, place_object: @route.destination, parent_form: f} %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

places/nested_places_form.html.erb

 <%= parent_form.fields_for record_name, place_object  do |t| %>                                                                                                               
  <%= record_name %>

  <% if place_object.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@place.errors.count, "error") %> prohibited this place from being saved:</h2>

      <ul>
      <% @place.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= t.label :name %><br />
    <%= t.text_field :name %>
  </div>
  <div class="field">
    <%= t.label :lat %><br />
    <%= t.text_field :lat %>
  </div>
  <div class="field">
    <%= t.label :lng %><br />
    <%= t.text_field :lng %>
  </div>
<% end %>                                                                                                                                                                     

Like I mentioned, the attributes of origin and destination always end up the same even when I put different values in blanks and submit from form.

How can I make this work?

  • 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-02T20:52:26+00:00Added an answer on June 2, 2026 at 8:52 pm

    Somehow you’ll need to differentiate between origin and destination in the database. If they both have the same class and are stored in the same table, there’s nothing to tell them apart. If you don’t want to change the existing relationships, you might need to use STI for this and make the origin and destination different classes:

    class OriginPlace < Place
    end
    
    class DestinationPlace < Place
    end
    
    class Route < ActiveRecord::Base
      ...
      has_one :origin, :class_name=>"OriginPlace"
      has_one :destination, :class_name=>"DestinationPlace"
      ...
    ene
    

    This will require a type field in the places table.

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

Sidebar

Related Questions

I am trying to loop through a bunch of documents I have to put
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka

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.