I have a form that displays information about a piece of equipment.
Among it’s attributes is its site and building. The trick is that a
site can have many buildings. Instead of storing both the site and the
building in the model, I just store the building because I can always
get the site from the building. If I store both of them in the model,
I would have to validate on each save if the combination is valid. And
I don’t want to go there.
So, a piece of equipment belongs to a building (through building_id)
and a building belongs to a site (through site_id).
Now, on the form I have two dependent selects. One for the building,
which depends on the selected value of the site, and one for the site
When the form is submitted, I need to remove site_id from the params
hash because otherwise I would get an error from ActiveRecord that
equipment.site_id is not a valid method (which would be correct) So,
Here’s the problem. When I select the site and don’t select a building
my model validation sees that building_id is missing and reloads the
form with an error. Great! BUT, I lose the previous selection for the
site because it’s not in the model.
What do I do to maintain the “view state” of something that is not in