如何使用两个属性belongs_to将一个 ActiveRecord 模型连接到另一个模型



请 我需要一些帮助来解决下面的问题。

我正在尝试连接两个活动记录模型,其中一个模型(作业(有两个属性,hiring_company_id和advertising_company_id,它们引用另一个模型(公司基础(。但是,我在保存作业模型时收到一个 sql 错误,没有这样的表:main.hiring_companies:。

我以这种方式编写代码,省略了一些属性以更简洁:

工作模式:

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

作业模型的活动记录迁移:

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

公司基本模式:

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

当我创建 Job 对象并尝试保存它时,我收到以下 sql 错误,ActiveRecord::StatementInvalid:SQLite3::SQLException:没有这样的表:main.hiring_companies。但是对于 advertising_company 属性,我没有收到相同的错误,并且代码是相同的。我做错了什么?

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

谢谢

何塞·费尔南多

我已经解决了这个问题,我在这里将答案发布给有类似问题的人。 该问题是由错误的关系名称引起的。正确的方法是:

工作模式:

class Job < ApplicationRecord
has_paper_trail on: [:update, :destroy]
belongs_to :job_type, inverse_of: :jobs
belongs_to :advertising_company, class_name: 'CompanyBase', inverse_of: :jobs
belongs_to :hiring_company, class_name: 'CompanyBase', inverse_of: :jobs
validates :hide_advertising_company, inclusion: { in: [true, false], message: :must_be_true_or_false }, if: lambda { advertising_company.present? }
end

作业模型的活动记录迁移:

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', index: true
t.belongs_to :hiring_company, class_name: 'CompanyBase', index: true
t.timestamps
end
end
end

公司基本模式:

class CompanyBase < ApplicationRecord
has_paper_trail on: [:update, :destroy]
has_many :jobs, dependent: :destroy, inverse_of: :advertising_company
has_many :jobs, dependent: :destroy, inverse_of: :hiring_company
end

此致敬意

何塞·费尔南多

最新更新