2017-08-06 74 views
0

請幫我解決下面的問題。如何使用belongs_to的兩個屬性將ActiveRecord模型連接到另一個模型

我試圖連接兩個activerecord模型,其中一個模型(Job)有兩個屬性,hiring_company_id和advertising_company_id,它引用了另一個模型(CompanyBase)。但是,我收到一個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 

的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。但是我沒有得到與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 

感謝,

何塞·費爾南多

回答

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 

的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 

最好的問候,

何塞費爾南多

相關問題