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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T15:45:55+00:00 2026-05-19T15:45:55+00:00

I have a Customers model: class Customer < ActiveRecord::Base has_many :phone_numbers end and a

  • 0

I have a Customers model:

class Customer < ActiveRecord::Base
  has_many    :phone_numbers
end

and a Phone Numbers model

class PhoneNumber < ActiveRecord::Base
  belongs_to :customer
end

In my view, I’m doing this:

<table id="customerSearch">
  <tr>
    <th>Last name</th>
    <th>First name</th>
    <th></th>
    <th></th>
    <th></th>
  </tr>
<% @customers.each do |customer| %>
  <tr>
    <td><%= customer.last_name %></td>
    <td><%= customer.first_name %></td>
    <td><%= link_to 'Show', customer %></td>
    <td><%= link_to 'Edit', edit_customer_path(customer) %></td>
  </tr>
<% end %>
</table>

and here is my controller for this action:

def index
  @customers = Customer.find(:all, :limit => 10, :order => 'last_name')
  flash.now[:notice] = 'Enter customer last or first name. Fields are case-sensitive.'
end

In the table in my view, I want to show the first phone number found for each customer listed in a phone number column – like:

Last Name    first Name    Phone

Smith        John          3258889322
Jones        Davey         3412555232

I’ve come up with multiple solutions to this problem, but none was very elegant. There must be a ‘Rails Way’ to do this that is really snazzy, as this seems like a common situation for a web application developer to be faced with.

  • 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-19T15:45:56+00:00Added an answer on May 19, 2026 at 3:45 pm

    In your view:

    <table id="customerSearch">
      <tr>
        <th>Last name</th>
        <th>First name</th>
        <th>Phone</th>
        <th></th>
        <th></th>
      </tr>
    <% @customers.each do |customer| %>
      <tr>
        <td><%= customer.last_name %></td>
        <td><%= customer.first_name %></td>
        <td><%= customer.phone_numbers.first.number if customer.phones.exists? %>
        <td><%= link_to 'Show', customer %></td>
        <td><%= link_to 'Edit', edit_customer_path(customer) %></td>
      </tr>
    <% end %>
    </table>
    

    Your association will take care of creating those phone-number-related attribute accessors for you. You can see all of the methods that are created for you with a has_many association here.

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

Sidebar

Related Questions

I have a nested form with the following models: class Incident < ActiveRecord::Base has_many
How do I model the following using Castle ActiveRecord? I have two classes, Customer
I have a Job model that belongs_to three users (customer, employee, and qa). The
I have these 3 models in models.py class Customer(models.Model): name = models.CharField(max_length=50) .... class
Say I have 2 tables: Customers and Orders. A Customer can have many Orders.
I have a database with this table: Customer - ID - Phone - Cell
I have a customer class with a sub-class address internal class Customer { public
I have some questions about customers about NF mode for DB2. Google had very
We have customers who, for unassailable reasons, cannot use SQL Server's built-in backup features
I have several customers where my WinForms app does not perform as well as

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.