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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T15:47:53+00:00 2026-06-14T15:47:53+00:00

I have an Address model and an Order model in my Rails app. An

  • 0

I have an Address model and an Order model in my Rails app. An order has a billing address and a delivery address. I’m trying to figure out the most efficient way of implementing this with a polymorphic association (other models will also have addresses in future) and this is the code I’ve come up with:

address.rb:

class Address < ActiveRecord::Base
  attr_accessible :line_1, :line_2, :city, :county, :postcode
  # Also has an 'address_type' attribute

  belongs_to :addressable, polymorphic: true
end

order.rb:

class Order < ActiveRecord::Base
  has_many :addresses, as: :addressable

  def delivery_address
    addresses.where(address_type: 'delivery').first
  end

  def delivery_address=(address)
    address.address_type = 'delivery'
    addresses << address
  end

  def billing_address
    addresses.where(address_type: 'billing').first
  end

  def billing_address=(address)
    address.address_type = 'billing'
    addresses << address
  end
end

Adding an address:

Order.first.address = Address.new(line_1: 'My House', city: 'Cityville, county: 'Hazzard')

This almost works except that trying to change an address by calling order.delivery_address= again just adds another address of type ‘delivery’. I’m also pretty sure that there must be a more elegant way of doing it and I’d really appreciate any advice.

  • 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-14T15:47:54+00:00Added an answer on June 14, 2026 at 3:47 pm
    has_one :delivery_address, as: :addressable,
             conditions: "addresses.address_type = 'delivery'", class_name: "Address"
    has_one :billing_address,  as: :addressable,
             conditions: "addresses.address_type = 'billing'", class_name: "Address"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a model that holds user address. This model has to have first_name
I have a model that contains an Address property and Latitude / Longitude properties.
I have a view model with an Email address property and a Confirm Email
My app has deals where orders are made once a minimum order amount is
You have models: class Order(Model): date = DateField(editable = False, auto_now_add=True) status = CharField(max_length
I have a business model called Customer which has many required properties (via DataAnnotations)
I am creating a Rails app (fairly new at this), and I have a
I have the following classes in my models file class HardwareNode(models.Model): ip_address = models.CharField(max_length=15)
I have a variable length collection of address models which I want to edit.
I have three models: class Address < ActiveRecord::Base has_many :jobs end class Category <

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.