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

Three models: class Customer < ActiveRecord::Base has_many :visits end class Visit < ActiveRecord::Base belongs_to
I have following model: class Customer < ActiveRecord::Base has_one :cart end class Cart <
I have the following rails model: class Customer < ActiveRecord::Base attr_accessible :firstname, :lastname, :email,
I have a nested form with the following models: class Incident < ActiveRecord::Base has_many
I have 3 models: class DropShipOrderLineItem < ActiveRecord::Base belongs_to :drop_ship_order belongs_to :line_item validates_associated :drop_ship_order
My Customer and Person models looks like this: class Customer < ActiveRecord::Base belongs_to :person
It seems simple, in my model I have: class CustomerAccount < ActiveRecord::Base acts_as_url :name
I have two models: class Customer(models.Model): (...) class CustomerMemo(models.Model): (...) customer = models.ForeignKey(Customer) text
I have a model called Customer in google app engine with python: class Customer(db.Model):
Suppose I have the view models below. public class CustomerListViewModel { public IEnumerable<CustomerViewModel> Customers

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.