【问题标题】:How to connect an ActiveRecord model to another model using two attributes with belongs_to如何使用带有 belongs_to 的两个属性将 ActiveRecord 模型连接到另一个模型
【发布时间】:2017-08-06 20:16:50
【问题描述】:

我需要一些帮助来解决以下问题。

我正在尝试连接两个 activerecord 模型,其中一个模型(Job)有两个属性,hiring_company_id 和 ads_company_id,它们引用另一个模型(CompanyBase)。但是,我收到一个 sql 错误,没有这样的表:main.hiring_companies:,保存 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

Job 模型的 ActiveRecord 迁移:

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

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

当我创建 Job 对象并尝试保存它时,我收到以下 sql 错误,ActiveRecord::StatementInvalid: SQLite3::SQLException: no such table: main.hiring_companies。但是我没有得到同样的错误广告公司属性,代码是一样的。我做错了什么?

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

谢谢,

何塞·费尔南多

【问题讨论】:

    标签: activerecord foreign-keys belongs-to


    【解决方案1】:

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

    工作模式:

    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
    

    Job 模型的 ActiveRecord 迁移:

    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
    

    CompanyBase 模型:

    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
    

    最好的问候,

    何塞·费尔南多

    【讨论】:

      猜你喜欢
      • 2019-11-08
      • 1970-01-01
      • 1970-01-01
      • 2017-04-24
      • 2020-08-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-30
      相关资源
      最近更新 更多