I am confused about the accepted way to update an association. Let's assume there are 2 models, Worker and Factory. When editing a Worker, the user is presented with a select_tag that contains a list of factories. My issue is the fact that the factory_id is not in the params[:worker][:factory_id] hash, but is instead in the params[:factory_id] hash. The end effect is that when my worker.update_attributes is called, the factory_id isn't updated because it isn't in the :worker hash.
My question is should I even be trying to get my factory_id into the params[:worker] hash through modifying my erb file, or should I be manually updating the value at params[:worker][:factory_id] with the value found in params[:factory_id]. Below is my erb code just in case there is something different I should be doing there. Also, the Factory class has_many workers and the Worker class belongs_to the Factory class if that helps.
<div>
<%= label_tag :factory , "Factory" %><br />
<%= select_tag "factory_id",
options_from_collection_for_select(@state_factories... %>
</div>
Any help is greatly appreciated.
Do it in the ERB file:
<%= select_tag "worker[factory_id]", options_from_collection_for_select(@state_factories... %>
Although, I'd use the select helper:
<%= select "worker", "factory_id", options_from_collection_for_select(@state_factories... %>
As that would automatically select the factory on edit.