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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T23:25:44+00:00 2026-05-20T23:25:44+00:00

I have a form that handles 2 models, Vehiculo and Poliza. This is how

  • 0

I have a form that handles 2 models, Vehiculo and Poliza. This is how I have them set up right now:

class Vehiculo < ActiveRecord::Base
  has_one :poliza
end

class Poliza < ActiveRecord::Base
  belongs_to :vehiculo
end

The create method on Vehiculo looks like this:

def create
    @vehiculo = Vehiculo.new(params[:vehiculo])
    @polizadeseguro = Polizadeseguro.new(params[:poliza])

respond_to do |format|
  if @vehiculo.save #&& @poliza.save

    format.html { redirect_to(@vehiculo, :notice => 'Vehiculo was successfully created.') }
    format.xml  { render :xml => @vehiculo, :status => :created, :location => @vehiculo }
  else
    format.html { render :action => "new" }
    format.xml  { render :xml => @vehiculo.errors, :status => :unprocessable_entity }
  end

end

The form on /vehiculos/new has a @fields_for part with the fields from poliza. When I submit the form, it is saving all the fields, but it is not assigning the just created id from vehiculo, to vehiculo_id on the Polizas table. After reading many questions about this online, It seems that it should save it “automagically” based on the relationships on the model. Is this true? If so, why isn’t it working? If not, what do I need to add to the create method so I resolve this?

Thanks!

Update:
After updating the create method with json as output as suggested here is what I get:

{
  "utf8"=>"✓",
  "authenticity_token"=>"tEhNC4J17h+KvNgXv1LLkVyufQwU2uAT18P7msQxiqA=",
  "vehiculo"=>{
    "marca_id"=>"2",
    "modelo_id"=>"4",
    "color"=>"Blanco",
    "ano"=>"2011",
    "chassis"=>"123456789",
    "placa"=>"G123456",
    "cliente_id"=>"1",
    "entaller"=>"0",
    "vip"=>"0"
  },
  "poliza"=>{
    "compania"=>"Comp1",
    "numeropoliza"=>"736458",
    "vencimiento(1i)"=>"2011",
    "vencimiento(2i)"=>"9",
    "vencimiento(3i)"=>"21"
  }
}

That’s the output, so at least it is getting the fields from the form, but it is not inserting them to the polizas table.

  • 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-20T23:25:44+00:00Added an answer on May 20, 2026 at 11:25 pm

    You need to make sure that your parent model accepts nested attributes for the child model:

    class Vehiculo < ActiveRecord::Base
      has_one :poliza
      accepts_nested_attributes_for :poliza
    end
    

    Assuming your form is set up correctly, your params will look something like this:

    params = {
      :vehiculo => {
        :field => "value",
        :another_field => "value",
        :poliza => {
          :poliza_field => "poliza value"
        }
      }
    }
    

    So all you should need in your controller is:

    def create
      @vehiculo = Vehiculo.new(params[:vehiculo])
    
      respond_to do |format|
        if @vehiculo.save #&& @poliza.save
          format.html { redirect_to(@vehiculo, :notice => 'Vehiculo was successfully created.') }
          format.xml  { render :xml => @vehiculo, :status => :created, :location => @vehiculo }
        else
          format.html { render :action => "new" }
          format.xml  { render :xml => @vehiculo.errors, :status => :unprocessable_entity }
        end
      end
    end
    

    [Update]

    Here’s what you’ll need to have to have this all work.

    As mentioned above, you need accepts_nested_attributes_for.

    Next, make sure your new action is building the child.

    class VehiculosController < ApplicationController
      def new
        @vehiculo = Vehiculo.new
        @vehiculo.build_poliza
      end
    
      def create
        vehiculo = Vehiculo.new(params[:vehiculo])
        if vehiculo.save
          redirect_to root_path, :notice => "Success"
        else
          redirect_to root_path, :alert => "Failure"
        end
      end
    end
    

    Finally, in your view, reference the child model using fields_for :child_model, as such:

    <%= form_for @vehiculo do |f| %>
      <p>Whatever Field: <%= f.text_field :whatever %></p>
      <%= f.fields_for :poliza do |p| %>
        <p>Polizo Field: <%= p.text_field :something %></p>
      <% end %>
    <% end %>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a form that handles editing of a domain model (A). This domain
I have a view which handles a form. This form has an event that
I have a single view that handles a lot of Models of type VoyagesViewModel,
I have the following model set up: class UserProfile(models.Model): Additional attributes for users. url
I have a ModelForm field that is based on the following Model: class Phrase(models.Model):
I have a code similar to this to handle a form submission: use \my-project\web\models\forms\RegisterOrganizationForm;
I currently have form that checks if a user has unsubmitted changes when they
I have a form that I'm submitting through AJAX. The form includes many fields,
I have a form that I want to be validated before showing it initially.
I have a form that sends money value e.g <input type=text name=amount value=N50,000.00 NGN

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.