title: How to connect an ActiveRecord model to another model using two attributes with belongs_to
body: <p>Please I would like some help to solve the problem below.</p>
  
  <p>I'm trying to connect two activerecord models, one of the models (Job) has two attributes, hiring_company_id and advertising_company_id, which reference the other model (CompanyBase). But, I'm getting an sql error, no such table: main.hiring_companies:, when saving the Job model.   </p>
  
  <p>I wrote the code this way, I omitted some attributes to be more concise:</p>
  
  <p>Job Model:</p>
  
  <pre><code>class Job &lt; 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
  </code></pre>
  
  <p>ActiveRecord migration for the Job model:   </p>
  
  <pre><code>class CreateJobs &lt; 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
  </code></pre>
  
  <p>CompanyBase model:</p>
  
  <pre><code>class CompanyBase &lt; 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
  </code></pre>
  
  <p>When I create the Job object and I try to save it I get the following sql error, ActiveRecord::StatementInvalid: SQLite3::SQLException: no such table: main.hiring_companies. But I don't get the same error for the advertising_company attribute, and the code is the same. What I am doing wrong ?</p>
  
  <pre><code>irb(main):018:0&gt; 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
  </code></pre>
  
  <p>Thanks,</p>
  
  <p>Jose Fernando</p>