2017-08-06 3 views
0

以下の問題を解決するためにいくつかお手伝いします。belongs_toで2つの属性を使用してActiveRecordモデルを別のモデルに接続する方法

2つのアクティブレコードモデルを接続しようとしていますが、モデル(ジョブ)の1つにhiring_company_idとadvertising_company_idという2つの属性があり、他のモデル(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:このようなテーブルはありません: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 

敬具、

ホセフェルナンド

関連する問題