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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T15:15:27+00:00 2026-06-06T15:15:27+00:00

What is the Rails way of implementing a view for a data model that

  • 0

What is the Rails way of implementing a view for a data model that contains elements with a has_many/belongs_to relationship?

Here is the model I want to show in list form:
Driver

class Driver < ActiveRecord::Base
  ...
  has_many :cars
end

Here is the model that should be listed underneath each driver:

Car

class Car < ActiveRecord::Base
  ...
  belongs_to :driver
end

I want to show a list of drivers with their respective cars.

This raises two questions:

1 . How do I most efficiently list both in one page? What would my controller and view have to look like? If I simply do

    @drivers = Driver.all

I do not seem to get access to the cars in the view.

2 . If I only list the drivers, and load the cars upon request using AJAX, would I use a cars_controller and a drivers_controller? How would I approach writing tests for this scenario?

  • 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-06T15:15:28+00:00Added an answer on June 6, 2026 at 3:15 pm

    Expanding on what you already have:

    //controller
      @drivers = Driver.all
    
      //view
      @drivers.each do |d|
       puts d.messages
       # OR
       d.messages.each do |m|
        puts m.title
       end
      end
    

    The same can be done inversely with @cars = Car.all.

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

Sidebar

Related Questions

I know that RAILS_ROOT is the old way of getting Rails.root and that it
What would be the best way to version a rails application? We want to
Is there a way to cache per-request data in Rails? For a given Rails/mongrel
Am learning rails the way most do, by implementing a blog. I've just put
This might sound odd, but is there a 'Rails way' to have a model
I have a rails app that I am looking into implementing videos. I know
What's the rails way to see posts nested beneath several associative models that a
What is the rails way of implementing a min max validator in Rails 3
Is there a rails-way way to validate that an actual record is unique and
What would be the rails way of implementing version control in my record management

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.