diff --git a/app/controllers/device_jobs_controller.rb b/app/controllers/device_jobs_controller.rb index d78590e..9e7a937 100644 --- a/app/controllers/device_jobs_controller.rb +++ b/app/controllers/device_jobs_controller.rb @@ -46,6 +46,6 @@ class DeviceJobsController < ApplicationController # Only allow a list of trusted parameters through. def device_job_params - params.require(:device_job).permit(:device_id, :jobs_id, :status) + params.require(:device_job).permit(:device_id, :job_id, :status) end end diff --git a/app/controllers/jobs_controller.rb b/app/controllers/jobs_controller.rb index f2f4695..0c1ff8d 100644 --- a/app/controllers/jobs_controller.rb +++ b/app/controllers/jobs_controller.rb @@ -46,6 +46,6 @@ class JobsController < ApplicationController # Only allow a list of trusted parameters through. def job_params - params.require(:job).permit(:job_id, :params, :type) + params.require(:job).permit(:job_id, :params, :job_type) end end diff --git a/app/models/device_job.rb b/app/models/device_job.rb index 98d460e..eea919c 100644 --- a/app/models/device_job.rb +++ b/app/models/device_job.rb @@ -1,2 +1,7 @@ class DeviceJob < ApplicationRecord -end + # self.primary_key = :jobs_id, :device_id + belongs_to :job + belongs_to :device + # :foreign_key => [:user_id, :group_id] + +end \ No newline at end of file diff --git a/app/models/job.rb b/app/models/job.rb index 12f5bd6..33a93a5 100644 --- a/app/models/job.rb +++ b/app/models/job.rb @@ -1,2 +1,4 @@ class Job < ApplicationRecord + + has_many :device_jobs end diff --git a/db/migrate/20211020095206_create_jobs.rb b/db/migrate/20211020095206_create_jobs.rb index 0e055f2..e182423 100644 --- a/db/migrate/20211020095206_create_jobs.rb +++ b/db/migrate/20211020095206_create_jobs.rb @@ -2,7 +2,7 @@ class CreateJobs < ActiveRecord::Migration[6.1] def change create_table :jobs do |t| t.json :params - t.string :type + t.string :job_type t.timestamps end diff --git a/db/migrate/20211021061911_create_device_jobs.rb b/db/migrate/20211021061911_create_device_jobs.rb index 890bdac..5f2d0ee 100644 --- a/db/migrate/20211021061911_create_device_jobs.rb +++ b/db/migrate/20211021061911_create_device_jobs.rb @@ -1,11 +1,19 @@ class CreateDeviceJobs < ActiveRecord::Migration[6.1] def change - create_table :device_jobs do |t| + create_table :device_jobs, id: false do |t| + # t.references :job_id, references: :job, foreign_key: true # the owner + # t.references :device_id, references: :device, foreign_key: true # the invitee t.integer :device_id - t.integer :jobs_id + t.integer :job_id t.string :status t.timestamps end + add_index :device_jobs, :job_id + add_foreign_key :device_jobs, :jobs, column: :job_id + add_index :device_jobs, :device_id + add_foreign_key :device_jobs, :devices, column: :device_id + # execute "ALTER TABLE device_jobs ADD PRIMARY KEY (device_id, jobs_id);" end end + diff --git a/db/schema.rb b/db/schema.rb index 7f44d09..23b7e9e 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,11 +10,21 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2021_10_20_101118) do +ActiveRecord::Schema.define(version: 2021_10_21_061911) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" + create_table "device_jobs", id: false, force: :cascade do |t| + t.integer "device_id" + t.integer "job_id" + t.string "status" + t.datetime "created_at", precision: 6, null: false + t.datetime "updated_at", precision: 6, null: false + t.index ["device_id"], name: "index_device_jobs_on_device_id" + t.index ["job_id"], name: "index_device_jobs_on_job_id" + end + create_table "devices", force: :cascade do |t| t.string "device_id" t.datetime "created_at", precision: 6, null: false @@ -23,9 +33,11 @@ ActiveRecord::Schema.define(version: 2021_10_20_101118) do create_table "jobs", force: :cascade do |t| t.json "params" - t.string "type" + t.string "job_type" t.datetime "created_at", precision: 6, null: false t.datetime "updated_at", precision: 6, null: false end + add_foreign_key "device_jobs", "devices" + add_foreign_key "device_jobs", "jobs" end diff --git a/test/controllers/jobs_controller_test.rb b/test/controllers/jobs_controller_test.rb index 9855fd1..2f93b61 100644 --- a/test/controllers/jobs_controller_test.rb +++ b/test/controllers/jobs_controller_test.rb @@ -12,7 +12,7 @@ class JobsControllerTest < ActionDispatch::IntegrationTest test "should create job" do assert_difference('Job.count') do - post jobs_url, params: { job: { job_id: @job.job_id, params: @job.params, type: @job.type } }, as: :json + post jobs_url, params: { job: { job_id: @job.job_id, params: @job.params, job_type: @job.job_type } }, as: :json end assert_response 201 @@ -24,7 +24,7 @@ class JobsControllerTest < ActionDispatch::IntegrationTest end test "should update job" do - patch job_url(@job), params: { job: { job_id: @job.job_id, params: @job.params, type: @job.type } }, as: :json + patch job_url(@job), params: { job: { job_id: @job.job_id, params: @job.params, job_type: @job.job_type } }, as: :json assert_response 200 end