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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T15:42:08+00:00 2026-06-18T15:42:08+00:00

A client has_many projects. A project belongs_to a client. How do I show in

  • 0

A client has_many projects. A project belongs_to a client.

How do I show in the index view, inside the @projects loop, which client that project belongs to?

This is what I’m trying to do in the view:

<% @projects.each do |project| %>
   <% project.client.name %>
<% end %>

Here’s all I have in the projects controller & I think this is where I’m stuck:

  def index
    @projects = Project.all
    @project = Project.new
    @clients = Client.select("DISTINCT name, id")

    respond_to do |format|
      format.html # index.html.erb
      format.json { render :json => @projects }
    end
  end

I got this working with a HABTM relationship, but I’m trying to do it w/ a has_many belongs_to relationship now instead.

  • 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-18T15:42:10+00:00Added an answer on June 18, 2026 at 3:42 pm

    Replace this

    <% @projects.each do |project| %>
       <% project.client.name %>
    <% end %>
    

    with

    <% @projects.each do |project| %>
       <%= project.client.name %>
    <% end %>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Client model which has many projects. In the project model I
If you have the following models: client has_many :projects project has_many :tasks tasks has_many
In my project I have a one to many relationship from Client -> Projects
our main client solution has 111 projects. When I first started on this team
A project has 3 tables : client, invoices, deliveries The relations are: client has_many
I am starting work on a project which has multiple websites for a client.
I have a project model that has_many :tasks . I added a nested resource
I am working on project that is near completion that handles a business' client
I'm building WPF client application. I'm following MVC pattern. The project solution has many
clients :has_many :project,:roles I have implement but its not a good implementation..

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.