Krawalla
Krawalla

Reputation: 198

Associate data tables and automatically updating them in Ruby

Basically, I've got 3 data tables. "jobs", "periods", and an associated table "JobPeriodAssociation".

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.

If I now add a period in the "periods" table, it automatically updates the "JobPeriodAssociation" table by adding a (predefined) 3rd period/job combination. I.a. j1t1, j1t2, j1t3, j2t1, j2t2, j2t3.

This is accomplished by a change action in the periods_controller

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 > 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, apples: 2, bananas: 2, milk: 2, beer: 2)
    }
  end
end

(+obviously some minor alterations in routes and the index views)

When creating a job, I create it with several params (see the image attached here: TABLES) As of now, the params don't get pushed to the JobPeriodAssociation table and I only figured out a way how to hard code them as can be seen in the first listing of this topic

    @new_period=Period.create!(name: name)
    @jobs.each { |jo|
      JobPeriodAssociation.create(job_id: jo.id, period_id: @new_period.id, apples: 2, bananas: 2, milk: 2, beer: 2)
    }

As a result, every newly created entry in the JobPeriodAssociation table has the params 2, 2, 2, 2. However, I would like to push the created jobs including its params to the JobPeriodAssociation as displayed in the image attached. Do you have any thoughts on that?

Thank you very much for your help!

Upvotes: 1

Views: 61

Answers (1)

fabOnReact
fabOnReact

Reputation: 5942

Have you though of doing a Join between Period and Job, which would be JobPeriodAssociation? Maybe I misunderstood your problem, I tell I am sorry. Please let's discuss it to help you solve it.

enter image description here

You can get a better idea of what i mean at the following link:

http://guides.rubyonrails.org/association_basics.html#the-has-and-belongs-to-many-association

If this is the problem your are trying to solve, then the best best way is a has_many though JobPeriodassociation. This is the link to the full explanation.

The join would consist in the following

Job Model

has_many  :jobperiodassociations

Period Model

has_many :jobperiodassociations

JobPeriodAssociation

belongs_to :job
belongs_to :period => all the entries of jobperiodassociations will be retrieved with the foreign key job_id in jobperiodassociations

Instances of job:

job = Job.new
job.jobperiodassociations = [] => all the entries of jobperiodassociations will be retrieved with the foreign key job_id in jobperiodassociations

Instances of period

period = Period.new
period.jobperiodassociations = []

Instances of jobperiodassociations have two foreign keys, one for job and one for period.

http://guides.rubyonrails.org/association_basics.html#the-has-many-through-association

Upvotes: 1

Related Questions