title: Associate data tables and automatically update them in Ruby
body: <p>Basically, I've got 3 data tables. "jobs", "periods", and an associated table "JobPeriodAssociation".</p>
  
  <p>Now, the JobPeriodAssociation displays every possible combination of jobs and periods. E.g. i've got 2 jobs (j1, j2) and 2 periods (t1, t2), the table will display 4 rows: j1t1, j1t2, j2t1, j2t2. </p>
  
  <p>If I now add a period in the "periods" table, it automatically updates the "JobPeriodAssociation" table by adding a (blank) 3rd period/job combination. I.a. j1t1, j1t2, j1t3, j2t1, j2t2, j2t3. </p>
  
  <p>This is accomplished by a change action in the periods_controller</p>
  
  <pre><code>def change
  @mynumber_of_periods = params[:mynewnumber].to_i
  @number_of_periods = Period.count
  number_of_missing_periods=@mynumber_of_periods - @number_of_periods
  
  if number_of_missing_periods &gt; 0
    @jobs = Job.all
    @nanns = Nann.all
    (1..number_of_missing_periods).each do |number|
      name="t#{number+@number_of_periods}"
      @new_period=Period.create!(name: name)
      @jobs.each { |jo|
        JobPeriodAssociation.create(job_id: jo.id, period_id: @new_period.id, demand: 1, wtp: 0, minage: 0, maxage: 0, genderjf: 0, genderjm: 0, genderji: 0, minval: 0, children: 0, dlj: 0, faj: 0, pcj: 0)
      }
      @nanns.each { |na|
        NannPeriodAssociation.create(nann_id: na.id, period_id: @new_period.id, available: 1, wage: 0, age: 0, value: 0, genderf: 0, genderm: 0, childrenn: 0, dl: 0, fa: 0, pc: 0)
        Matching.create(nann_id: na.id, period_id: @new_period.id)
      }
    end
  end
  </code></pre>
  
  <p>(+obviously some minor alterations in routes and the index views)</p>
  
  <p>I did the same for Jobs, meaning that changing the amount of jobs results in automatic alterations in the JobPeriodAssociation.</p>
  
  <pre><code>  def change
  @mynumber_of_jobs = params[:mynewnumber].to_i
  @number_of_jobs = Job.count
  number_of_missing_jobs=@mynumber_of_jobs - @number_of_jobs
  
  if number_of_missing_jobs &gt; 0
    @periods = Period.all
    (1..number_of_missing_jobs).each do |number|
      name="j#{number+@number_of_jobs}"
      @new_job=Job.create!(name: name)
      @periods.each { |per|
        JobPeriodAssociation.create(period_id: per.id, job_id: @new_job.id, demand: 1, wtp: 0, minage: 0, maxage: 0, genderjf: 0, genderjm: 0, genderji: 0, minval: 0, children: 0, dlj: 0, faj: 0, pcj: 0)
      }
    end
  end
  
  if number_of_missing_jobs &lt; 0
    (@mynumber_of_jobs +1 ..@number_of_jobs).each do |number|
      name="j#{number}"
      Job.find_by_name(name).destroy
    end
  end
  </code></pre>
  
  <p>Now the problem is this: I've also got an option to simply create a new job.</p>
  
  <pre><code>  def new
  @job = Job.new
    end
  </code></pre>
  
  <p>The corresponding form to create a new job looks like this:</p>
  
  <pre><code>&lt;%= form_for(@job) do |f| %&gt;
  &lt;% if @job.errors.any? %&gt;
  &lt;div id="error_explanation"&gt;
    &lt;h2&gt;&lt;%= pluralize(@job.errors.count, "error") %&gt; prohibited this job from     being saved:&lt;/h2&gt;
  
    &lt;ul&gt;
    &lt;% @job.errors.full_messages.each do |message| %&gt;
      &lt;li&gt;&lt;%= message %&gt;&lt;/li&gt;
    &lt;% end %&gt;
    &lt;/ul&gt;
   &lt;/div&gt;
  &lt;% end %&gt;
  
  &lt;div class="field"&gt;
  &lt;%= f.label :name %&gt;&lt;br&gt;
  &lt;%= f.text_field :name %&gt;
  &lt;/div&gt;
  &lt;div class="field"&gt;
  &lt;%= f.label :demand %&gt;&lt;br&gt;
  &lt;%= f.number_field :demand %&gt;
  &lt;/div&gt;
  
  .
  .
  .
  
  &lt;div class="field"&gt;
  &lt;%= f.label :pcj %&gt;&lt;br&gt;
  &lt;%= f.number_field :pcj %&gt;
  &lt;/div&gt;
  
  
  &lt;div class="actions"&gt;
  &lt;%= f.submit %&gt;
  &lt;/div&gt;
  &lt;% end %&gt;
  </code></pre>
  
  <p>If I create a new job and not just "change" the amount of jobs, I want this change to also apply to the JobPeriodAssociation table but and can't seem to find a way. </p>
  
  <p>Very thankful for any suggestions!</p>