Assume I have 2 models: Device and Setting. I need to track and control the settings for each device. The number of devices and settings will grow over time. Clearly a many to many situation so I was going to use the "has_many :through" approach with a 3rd column for the setting value in the device_settings table.
My challenge is that as new settings are added I want to ensure that all devices have all settings populated in the device_settings table (the Settings model has a default_value attribute that can be used to populte the device_settings.value attribute). Once a setting is added to the table its easy to ensure that new devices or edited devices insert the new setting row. But is there a clean way to retro fit existing devices?
At some point the system will update the device settings and I want to avoid complex logic to handle missing values during this process. I think its better to enforce that the bridge table has all the rows in the first place.
I can, and have, written some ugly code (loops etc) to populate the device_settings table on the :after_save on the Setting model but I am looking for something cleaner.
Thank you