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 trialbessiebarnes
88,923 PointsHow 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.
class Car < ApplicationRecord
# YOUR CODE HERE
has_many :vehicle_id
has_many :vehicle_type
end
class Truck < ApplicationRecord
# YOUR CODE HERE
end
2 Answers
Jake Dewan
7,653 PointsFirst 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
Jeremiah Shafer
12,388 PointsThe 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
bessiebarnes
88,923 Pointsbessiebarnes
88,923 PointsThank you, Jake, for the help on this challenge.