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」を検索しています。どうすればこの修正を行うことができますか?