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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T00:01:51+00:00 2026-05-13T00:01:51+00:00

I’m not sure if what I’m even trying to do is possible but here

  • 0

I’m not sure if what I’m even trying to do is possible but here goes.

I have an SQL database with the following tables defined (showing only relevant tables in SQL):

CREATE TABLE customers(
    id integer NOT NULL UNIQUE,
    name vachar(25) NOT NULL,
    surname vachar(25) NOT NULL,
    password vachar(20) NOT NULL,
    email_address vachar(1024) NOT NULL,
    home_phone vachar(15),
    mobile_phone vachar(15),
    office_phone vachar(15),
    billing_address_id integer NOT NULL,
    postal_address_id integer,
    FOREIGN KEY (billing_address_id) REFERENCES addresses(id),
    FOREIGN KEY (postal_address_id) REFERENCES addresses(id),
    PRIMARY KEY (id));

CREATE TABLE addresses(
    id integer NOT NULL UNIQUE,
    line1 vachar(100) NOT NULL,
    line2 vachar(100),
    state vachar(30) NOT NULL,
    postcode vachar(10) NOT NULL,
    country_id vachar(3) NOT NULL,
    PRIMARY KEY (id));

CREATE TABLE orders(
    id integer NOT NULL UNIQUE,
    customer_id integer NOT NULL UNIQUE,
    order_date date NOT NULL,
    postal_address_id integer NOT NULL UNIQUE,
    FOREIGN KEY (customer_id) REFERENCES customers(id),
    PRIMARY KEY (id));

As you can see, the “customers” table defines a one-to-two relationship with addresses (one for billing address and one for postal/shipping address). The idea here being two fold:

  1. Saves duplicating address fields in the customers table by using relationships to address table.
  2. Later I can use the address ID to easily fill out the shipping address for an “order”.

Now I want to model this using Active Records with rails. So far I have the following:

1) The “Customer” model:

class Customer < ActiveRecord::Base
    has_one :postal_address, :class_name => 'Address', :foreign_key => :postal_address_id
    has_one :billing_address, :class_name => 'Address', :foreign_key => :billing_address_id
    accepts_nested_attributes_for :postal_address, :billing_address, :allow_destroy => true
end

2) The address model (default):

class Address < ActiveRecord::Base
end

3) The customer controller (only relevant methods shown, i.e. new & create):

class CustomersController < ApplicationController

  # GET /customers/new
  # GET /customers/new.xml
  def new
    @customer = Customer.new
    @customer.postal_address = Address.new
    @customer.billing_address = Address.new

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @customer }
    end
  end

  # POST /customers
  # POST /customers.xml
  def create
    @customer = Customer.new(params[:customer])

    respond_to do |format|
      if @customer.save
        flash[:notice] = 'Customer was successfully created.'
        format.html { redirect_to(@customer) }
        format.xml  { render :xml => @customer, :status => :created, :location => @customer }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @customer.errors, :status => :unprocessable_entity }
      end
    end
  end

end

3) My nested form for creating a new customer with billing address as well.

<% form_for(@customer) do |f| %>
  <%= f.error_messages %>

  <%= f.label :name, 'Name:' %>
  <%= f.text_field :name %>

  <%= f.label :surname, 'Surname:' %>
  <%= f.text_field :surname %>

  <br>

  <%= f.label :email_address, 'Email:' %>
  <%= f.text_field :email_address %>

  <%= f.label :confirm_email_address, 'Confirm Email:' %>
  <input id="confirm_email_address" type="text" />

  <br>

  <%= f.label :password, 'Password:' %>
  <%= f.text_field :password %>
  <%= f.label :confirm_password, 'Confirm Password:' %>
  <input id="confirm_password" type="password" %>

  <br>

  <%= f.label :home_phone, 'Home Phone:' %> 
  <%= f.text_field :home_phone %>

  <%= f.label :mobile_phone, 'Mobile Phone:' %>
  <%= f.text_field :mobile_phone %>

  <%= f.label :office_phone, 'Office Phone:' %>
  <%= f.text_field :office_phone %>

  <br>

  <% f.fields_for :billing_address do |billing_form| %>

    <%= billing_form.label :line1, 'Billing Address:' %>
    <%= billing_form.text_field :line1 %>

    <br>

    <%= billing_form.text_field :line2 %>

    <br>

    <%= billing_form.label :state, 'State / Province / Region:' %>
    <%= billing_form.text_field :state %>

    <br>

    <%= billing_form.label :postcode, 'Postcode / ZIP:' %>
    <%= billing_form.text_field :postcode %>

    <br>

    <%= billing_form.label :country_id, 'Country:' %>
    <%= billing_form.text_field :country_id %>

  <% end %>

  <p>
    <%= f.submit 'Create' %>
  </p>
<% end %>

Now to the problem. When I fill out this form and proceed to creating the new record I get the following error:

SQLite3::SQLException: customers.billing_address_id may not be NULL: INSERT INTO "customers" ("name", "office_phone", "billing_address_id", "postal_address_id", "home_phone", "surname", "password", "email_address", "mobile_phone") VALUES('Michael', '', NULL, NULL, '93062145', 'Fazio', '9npn4zicr', 'michael.fazio@me.com', '')

From this I understand that the billing address is not being created before the customer. I thought (probably very naively) that active record would recognize the relationship between a customer and address record and do the correct operation to create the new records. This is obviously not the case.

How can I make this so? I’m assuming logic needs to be in the customer controller to save the address record first then get the ID for that record to use in the customer controller. All within a transaction? Or maybe I have just modeled my DB in a bad way?

Hope that all this code was not too much but I wanted to give as much context as possible.

  • 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-13T00:01:51+00:00Added an answer on May 13, 2026 at 12:01 am

    Round 2:

    Ok, so I hope this will now help you. The way that you are implementing the dual address in one table isn’t exactly the “rails” way. It always goes that if you want to do something you’ve got to do it like DHH. So rails has STI (Single Table Inheritance) where you can have one super class with many classes inheriting from that.

    In your case, it shouldn’t be too much work (I hope) to move this paradigm.

    Step 1: Cut a hole in a box

    Step 2: Update your migration files. You want the addresses table to have the key to it corresponding Customer. Then take out the billing_address_id and shipping_address_id columns in the Customer table because we don’t need these anymore.

    You also want to add a field named type (if type is already taken there is a work around). Something like this:

    create_table :addresses do |t|
      t.string :line1
      t.string :line2
      t.string :state
      t.integer :postcode
      t.integer :country_id
      t.integer :customer_id
      t.integer :type
    
      t.timestamps
    

    Step 3: Update your models. Change your customer class to look like so:

    class Customer < ActiveRecord::Base
      has_one :postal_address
      has_one :billing_address
      accepts_nested_attributes_for :postal_address, :billing_address, :allow_destroy => true
    

    Then you’ll want to create two new files in the models directory: billing_address.rb and postal_address.rb. They should look like this:

    class BillingAddress < Address
      belongs_to :customer
    end
    
    class PostalAddress < Address
      belongs_to :customer
    end
    

    Step 4: Update Controllers. Now the only controller you showed in your question was customer_controller.rb but, fyi, this can apply for really anywhere. You want to replace Address.new with a call to instantiate either Shipping or Billing Addresses.

    def new
      @customer = Customer.new
      @customer.postal_address = PostalAddress.new
      @customer.billing_address = BillingAddress.new
    
      respond_to do |format|
        format.html # new.html.erb
        format.xml  { render :xml => @customer }
      end
    end
    

    Hopefully this actually works and it makes up for my abysmal attempt earlier 😉

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
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 have a reasonable size flat file database of text documents mostly saved in
I am trying to loop through a bunch of documents I have to put
Is it possible to replace javascript w/ HTML if JavaScript is not enabled on
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
Basically, what I'm trying to create is a page of div tags, each has

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.