w3hello.com logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML videos Categories
Attach generic records (like chipset) to a model (motherboard), without creating new record for every single record (motherboard)?

Up until now you had a one-to-many relationship with your models. With Motherboard, you have a many to many relationship.

Given two tables

| *motherboard* |  | * sataport * |
| Intel         |  | SATA 6G      |
| AMD           |  | SATA 3G      |

If Intel can have Sata 6G and 3G and AMD can also have SATA 6G and 3G, you will have to create a third table called motherboard_sataport with

| motherboard_id | sataport_id |
| 1              | 2           |
| 1              | 1           |


These are called JoinTables and rails can generate one for you. First create the motherboard table, then the sataport table. Then create a migration using

rails g migration CreateJoinTableMotherboardSataport motherboard

More info on rails migrations here and also go through the different types of associations in rails here

© Copyright 2018 w3hello.com Publishing Limited. All rights reserved.