0

Railsの「タイプ」パラメーターを使用してDB内のテーブルを共有し、それらを分離する2つのモデル(PersonとCustomer)があります。

# Person.rb
class Person < ActiveRecord::Base
  ...
end

# Customer.rb
class Customer < Person
  has_many :orders
end

また、注文テーブル:

class Order < ActiveRecord::Base
  belongs_to :customer
end

過去 90 日以内に注文した顧客を取得するテストを実行しています。

# Inside of Customer.rb
  def self.ordered_in_last_90_days
    scoped.joins(:orders).where('orders.created_at > ?', 90.days.ago)
  end

しかし、次のエラーが表示されます。

ActiveRecord::StatementInvalid:
   SQLite3::SQLException: no such column: orders.customer_id: SELECT "people".* FROM "people" INNER JOIN "orders" ON "orders"."customer_id" = "people"."id" WHERE "people"."type" IN ('Customer') AND (orders.created_at > '2013-06-18 16:47:44.726372')

ジョインは、「orders.person_id」を検索する必要があるときに、「orders.customer_id」を検索しています。どうすればこの修正を行うことができますか?

4

1 に答える 1

3

Foreign_key を指定する必要があります。

 class Customer < Person
   has_many :orders, foreign_key: person_id
 end
于 2013-09-16T17:03:53.233 に答える