2013-09-16 25 views
0

我有兩個模型(Person和客戶)共享使用Rails的‘類型’參數在一個數據庫中的表將它們分開:「沒有這樣的列」錯誤的模型類型的連接範圍

# 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.customer_id」時,應該尋找「orders.person_id」。我怎樣才能做出這個修正?

回答

3

你需要指定foreign_key:

class Customer < Person 
    has_many :orders, foreign_key: person_id 
end 
+0

啊我明白了。我試圖通過將'foreign_key'屬性添加到'Order'模型。顯然我需要將其添加到客戶。感謝您的快速幫助! –

相關問題