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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T12:03:46+00:00 2026-06-13T12:03:46+00:00

I have some models and have views code that look like the following class

  • 0

I have some models and have views code that look like the following

class Address < ActiveRecord::Base
  belongs_to :customer
end

class Customer < ActiveRecord::Base
  has_one :address
  has_many :invoices
end

class Invoice < ActiveRecord::Base
  belongs_to :customer
end

This code shows a simple invoice structure, with a customer who has a single address.
The view code to display the address lines for the invoice would be as follows:

<%= @invoice.customer.name %>
<%= @invoice.customer.address.street %>
<%= @invoice.customer.address.city %>,
<%= @invoice.customer.address.state %>
<%= @invoice.customer.address.zip_code %>

above view code is not ideal. For proper encapsulation, the
invoice should not reach across the customer object to the street attribute of the
address object. Because if, for example, in the future your application were to change
so that a customer has both a billing address and a shipping address, every place in
your code that reached across these objects to retrieve the street would break and
would need to change. How can I avoid this problem ?

  • 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-13T12:03:48+00:00Added an answer on June 13, 2026 at 12:03 pm

    A simples solution would be the customer have a method that will return its main address, like:

    class Customer < ActiveRecord::Base
      def main_address
        self.address
      end
    end
    

    If you access its address only by this method, when it has more than one address it is necessary just change the main_address method to do whatever you want to.

    Edit 1:

    Another option would be use the delegate as suggested by @soundar

    class Invoice < ActiveRecord::Base
      belongs_to :customer
      delegate :address, :to => :customer
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some models that are laid out like so (with a lot more
I have some code that I need to access both inside my models as
Generally, MVC frameeworks have a structure that looks something like: /models /views /controllers /utils
I have following line code in my view: <td> Model.some_instance_method(args) </td> I would like
I have a simple model class Ad < ActiveRecord::Base has_many :ad_items end class AdItem
I have a Rails application with several models-views-controllers which have some similar characteristics, for
I have some views, well bound to their respective models: image.handlerbars: URL:{{view Ember.TextField valueBinding=EvEditor.imageModel.imgUrl
I have a Django query and some Python code that I'm trying to optimize
I have some models; Vocabularies (tags lists), Labels (tags) and different articles types. These
I have some models witch are using Doctrine nestedset feature. I want to add

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.