S'il vous plaît je voudrais de l'aide pour résoudre le problème ci-dessous. J'essaie de connecter deux modèles d'enregistrement actifec, l'un des modèles (Job) a deux attributs, hiring_company_id et advertising_company_id, qui référence l'autre modèle (CompanyBase). Mais, je reçois une erreur sql, pas une telle table: main.hiring_companies:, lors de l'enregistrement du modèle Job.Comment connecter un modèle ActiveRecord à un autre modèle en utilisant deux attributs avec belongs_to
j'ai écrit le code de cette façon, j'omis certains attributs pour être plus concis:
Modèle Job:
class Job < ApplicationRecord
has_paper_trail on: [:update, :destroy]
belongs_to :job_type, inverse_of: :jobs
belongs_to :advertising_company, class_name: 'CompanyBase', foreign_key: :advertising_company_id, inverse_of: :jobs
belongs_to :hiring_company, class_name: 'CompanyBase', foreign_key: :hiring_company_id, inverse_of: :jobs
validates :hide_advertising_company, inclusion: { in: [true, false], message: :must_be_true_or_false }, if: lambda { advertising_company.present? }
end
migration ActiveRecord pour le modèle d'emploi:
class CreateJobs < ActiveRecord::Migration[5.1]
def change
create_table :jobs do |t|
t.belongs_to :job_type, foreign_key: true
t.belongs_to :advertising_company, class_name: 'CompanyBase', foreign_key: :advertising_company_id
t.belongs_to :hiring_company, class_name: 'CompanyBase', foreign_key: :hiring_company_id
t.timestamps
end
end
end
modèle CompanyBase :
class CompanyBase < ApplicationRecord
has_paper_trail on: [:update, :destroy]
has_many :jobs, foreign_key: :advertising_company_id, dependent: :destroy, inverse_of: :company_base
has_many :jobs, foreign_key: :hiring_company_id, dependent: :destroy, inverse_of: :company_base
end
Lorsque je crée l'objet Tâche et que j'essaie de le sauvegarder, j'obtiens l'erreur SQL suivante: ActiveRecord :: StatementInvalid: SQLite3 :: SQLException: aucune table de ce type: main.hiring_companies. Mais je n'obtiens pas la même erreur pour l'attribut advertising_company, et le code est le même. Qu'est ce que je fais mal ?
irb(main):018:0> jb.save!
(0.3ms) begin transaction
SQL (1.6ms) INSERT INTO "jobs" ("country_w_id", "job_type_id", "advertising_company_id", "hiring_company_id", "position", "handicapped_only", "hide_advertising_company", "hide_hiring_company", "hide_salary", "description", "requisites", "salary_from", "salary_to", "work_time", "created_at", "updated_at") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) [["country_w_id", 1], ["job_type_id", 1], ["advertising_company_id", 1], ["hiring_company_id", 1], ["position", "Gerente"], ["handicapped_only", "f"], ["hide_advertising_company", "f"], ["hide_hiring_company", "f"], ["hide_salary", "f"], ["description", "descrição"], ["requisites", "requisitos"], ["salary_from", 10], ["salary_to", 100], ["work_time", "full"], ["created_at", "2017-08-06 19:03:18.295519"], ["updated_at", "2017-08-06 19:03:18.295519"]]
(0.1ms) rollback transaction
ActiveRecord::StatementInvalid: SQLite3::SQLException: no such table: main.hiring_companies: INSERT INTO "jobs" ("country_w_id", "job_type_id", "advertising_company_id", "hiring_company_id", "position", "handicapped_only", "hide_advertising_company", "hide_hiring_company", "hide_salary", "description", "requisites", "salary_from", "salary_to", "work_time", "created_at", "updated_at") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
from (irb):18
i
Merci,
Jose Fernando