0
我有以下2個類。rails 3 activerecord - for:連接錯誤的sql生成
class Customer < ActiveRecord::Base
set_table_name "customer"
set_primary_key "customerId"
has_many :new_orders, :foreign_key => "customerid", :primary_key => "customerId", :class_name => "NewOrder"
end
class NewOrder < ActiveRecord::Base
set_table_name "viewNewOrders"
set_primary_key "orderid"
belongs_to :customer, :foreign_key => "customerid", :primary_key => "customerId"
end
我無法觸摸數據庫來更改任何表,視圖或列名稱。
這完美的作品:
new_orders_last_2_weeks =
NewOrder.where("orderdate >= :start and orderdate < :end",
{:start => period_start, :end => period_end})
然而這樣的:
new_orders_last_2_weeks =
NewOrder.joins(:customer).where("orderdate >= :start and orderdate < :end",
{:start => period_start, :end => period_end})
產生這種錯誤的SQL:
SELECT `viewNewOrders`.* FROM `viewNewOrders` INNER JOIN `customer` ON `customer`.`customerId` IS NULL WHERE (orderdate >= '2010-09-02' and orderdate < '2010-09-16')
我怎樣才能得到它來生成內加入適當的條件?
謝謝。