Welcome to the Treehouse Community
The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.
Kevin Korte28,135 Points
Need help building my Active Record schema
So I need help building out my schema for a rails app.
In one table, I'll store parts. I am considering using Single Table Inheritance here, since for parts I could have oil filters, air filters, spark plugs, etc, all which would have a brand, part_number, and type.
I'd also have a table of machines, most likely a title like Year Make Model.
Each part has many machines, and each machine has many parts. It would seem I would want to use a has many through relationship with a join table.
On the view, I need to be able to separate out the parts based on their type. So loop through and grab all air filters that are associated with that machine, than loop through and grab all oil filters etc.
I initially thought I wanted an array of part_id numbers for each type stored with the machine in the machine table, but that seems frowned upon.
Am I on the right track designing my schema? If all goes well, this would hold 10's of thousands of parts, and thousands of machines, and I need an easy way to tie a part number to a machine.
Reverse search would also be a feature I would want to look at; giving the user the option to enter a part number, and get a list of all machines that it is associated with.
Artem Prytkov11,932 Points
I think your idea is generally right: just don
t forget to make indexes on bridge table and use "include" method in ActiveRecord in order to avoid n+1 queries. Not so sure about STI - its useful when you have models with same data but different methods? but in your case all objects in table should be same.
It might be better to extract third table PartType where you will store types. This way you can easily get your part details with query like