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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T17:51:27+00:00 2026-05-27T17:51:27+00:00

INTRO model A carname.rb class Carname < ActiveRecord::Base belongs_to :car has_many :carmodels, :dependent =>

  • 0

INTRO

model A

carname.rb

    class Carname < ActiveRecord::Base
        belongs_to :car
        has_many :carmodels, :dependent => :destroy
...

model B

carmodel.rb

    class Carmodel < ActiveRecord::Base
      belongs_to :carname
      belongs_to :car
...

carname fields: id, name

carmodel fields: id, name, carname_id

The problem:

I add a car name for ex BMW, then I can add carmodels for ex 3 series and select that this belongs to BMW. Works good in different views, but I want it to display the carmodels in the index view of carname, something like this

in http://www.whateverdomain.com/carnames

BMW  
       3 series
       5 series
       7 series

Mercedes
       E class
       S class
       C class

I have carnames displayed, not a big deal:

<% @carnames.each do |carname| %>
  <%= carname.name %>
<% end %>

but I can’t think off how to add models nex to it.
In carmodels index view I managed to display each carmodels with the carname that it belongs_to next to the carmodel

<% @carmodels.each do |carmodel| %>
      <%= carmodel.name %>
      <%= carmodel.carname.name %>
      <%= link_to 'Edit', edit_carmodel_path(carmodel) %>
      <%= link_to "Delete", carmodel, :confirm => 'Are you sure?', :method => :delete %>
  <% end %>

so in this html on screen looks like

3 series | BMW
5 series | BMW
7 series | BMW
C class  | Mercedes
E class  | Mercedes
etc..

if any other info is needed I’ll share it. Thank you for your time.

  • 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-27T17:51:28+00:00Added an answer on May 27, 2026 at 5:51 pm

    Try this code in your carnames/index.html.erb file:

    <% @carnames.each do |carname| %>
        <h1><%= carname.name %></h1>
        <ul>
        <%- carname.carmodels.each do |carmodel| %>
            <li><%= carmodel.name %></li>
        <%- end %>
        </ul>
    <% end %>
    

    It will add every carname and their car models as an unordered list.

    Edit: If you want to display carmodel.name | carname.name you can do:

    <% @carnames.each do |carname| %>
        <ul>
        <%- carname.carmodels.each do |carmodel| %>
            <li><%= carmodel.name %> | <%= carname.name %></li>
        <%- end %>
        </ul>
    <% end %>
    

    Note: Why does a Carname belong to a Car? Wouldn’t it be easier to assign a name attribute to the Car class?

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

Sidebar

Related Questions

i have a meeting.rb and user.rb model meeting.rb class Meeting < ActiveRecord::Base attr_accessible :intro,
Say that a Rails app has a Car and an Order model. The Car
I am having difficulties implementing the codeigniter pagination class. I have created my model,
Has anyone tried the ActiveRecord Intro Sample with C# 3.5? I somehow have the
Intro: I am working on a car dealer project.. sells every brand of cars
Intro: I am working on a Car dealer application so I have a table
From: http://agiletoolkit.org/learn/understand/model/add class Model_MyModel extends Model_Table { function init(){ parent::init(); $this->addField('name'); } } From:
My program class has: Application.Run(new Form1()); in form1 class I have: model = new
If i put an object named foo into model in Spring controller, and want
I have data coming into my Model, how do I setup to insert the

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.