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

  • Home
  • SEARCH
  • 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 3348472
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T01:32:48+00:00 2026-05-18T01:32:48+00:00

I’m having trouble wrapping my head around this issue. I have a business model

  • 0

I’m having trouble wrapping my head around this issue.

I have a business model and an address model. To make things more complicated, I also have a location model. A business can have multiple locations, but only one mailing address.

Here is a simple object chart.

Business
-Name
-MailingAddress

Location
-Business
-Name
-Address

I want to reuse the Address model for both the Businesses & Locations. What do I need to do for this. If it helps, I’m using MySQL.

First how to I structure the models? Do I need has_many :location,:business (for Location/Business)?
Then how do I nest the forms for creating addresses into new Business/Location forms.

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-18T01:32:49+00:00Added an answer on May 18, 2026 at 1:32 am

    I’ll start with some basic model code, then move onto controller, then views.

    First the models:

    #app/models/business.rb
    class Business < ActiveRecord::Base
      has_one :mailing_address, :class_name => "Address", :dependent => :destroy
      accepts_nested_attributes_for :mailing_address
    
      has_many :locations
      accepts_nested_attributes_for :locations
    end
    
    #app/models/location.rb
    class Location < ActiveRecord::Base
      has_one :address, :dependent => :destroy
      accepts_nested_attributes_for :address
    
      belongs_to :business
    end
    
    #app/models/address.rb
    class Address < ActiveRecord::Base
      belongs_to :business
      belongs_to :location
    end
    

    Now the controller new action:

    #app/controllers/businesses_controller.rb#new
    def new
      @business = Business.new
    
      mailing_address = @business.build_mailing_address
      3.times do
        location = @business.locations.build
        address = location.build_address
      end
      respond_to do |format|
        format.html # new.html.erb
        format.xml  { render :xml => @business }
      end
    end
    

    Finally the views:

    #app/views/businesses/_form.html.erb
    <%= form_for(@business) do |f| %>
      <div class="field">
        <%= f.label :name %><br />
        <%= f.text_field :name %>
      </div>
      <%= f.fields_for :mailing_address do |builder| %>
        <h3>Mailing Address</h3>
        <%= render "address_fields", :f => builder %>
      <% end %>
      <%= f.fields_for :locations do |builder| %>
        <h3>Location</h3>
        <%= render "locations_fields", :f => builder %>
        <%= builder.fields_for :address do |mini_builder| %>
          <h3>Location Address</h3>
          <%= render "address_fields", :f => mini_builder %>
        <% end%>
      <% end %>
      <div class="actions">
        <%= f.submit %>
      </div>
    <% end %>
    

    address partial:

    #app/views/businesses/_address_fields.html.erb
    <p>
      <%= f.label "Street Address"%>
      <%= f.text_field :street %>
      <%= f.label "City"%>
      <%= f.text_field :city %>
      <%= f.label "State"%>
      <%= f.text_field :state %>
      <%= f.label "Zip"%>
      <%= f.text_field :zip %>  
    </p>
    

    location partial:

    #app/views/businesses/_location_fields.html.erb
    <p>
      <%= f.label "Location Name"%>
      <%= f.text_field :name%>
    </p>
    

    There are a couple trickier components in this whole thing. You have to keep in mind that building a has_one and a has_many child object is a different method in the controller. Also, you need to define the class properly for the mailing address relationship.

    If you wanted populate the business mailing address from a collection of the locations associated with the businesses addresses, then you could add a method in the Business model that did something like this:

    #app/models/business.rb#possible_mailing_addresses
    def possible_mailing_addresses
      Location.where(:business_id=>self.id).joins(:address)
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

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
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
This could be a duplicate question, but I have no idea what search terms
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I have just tried to save a simple *.rtf file with some websites and

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.