Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby Active Record Associations in Rails More Association Options Polymorphic "Has Many" Associations

bessiebarnes
bessiebarnes
88,270 Points

How do I Update the Car and Truck model classes so that each is treated as a "vehicle" which has_many Part instances?

I'm stuck on this challenge.

Update the Car and Truck model classes so that each is treated as a "vehicle" which has_many Part instances.

class Car, Parts < ApplicationRecord # YOUR CODE HERE has_many :vehicle_id has_many :vehicle_type end

We couldn't find any association from the Car class to the Part class. <---- This is the error I got from the above code.

models/car.rb
class Car < ApplicationRecord
  # YOUR CODE HERE
 has_many :vehicle_id
 has_many :vehicle_type 
end
models/truck.rb
class Truck < ApplicationRecord
  # YOUR CODE HERE
end

2 Answers

Jake Dewan
Jake Dewan
7,653 Points

First off what you need to do is to put the "Parts" instance in the has_many and then do it as a "Vehicle" instance so your code should look like this under the Car and Truck class.

class Car < ApplicationRecord has_many :parts, :as=> :vehicle end

class Truck < ApplicationRecord has_many :parts, :as=> :vehicle end

bessiebarnes
bessiebarnes
88,270 Points

Thank you, Jake, for the help on this challenge.

Jeremiah Shafer
Jeremiah Shafer
12,388 Points

The syntax of the above code is incorrect. It should be:

class Car < ApplicationRecord
    has_many :parts, as: :vehicle
end
class Truck < ApplicationRecord
    has_many :parts, as: :vehicle
end