views:

53

answers:

1

i have the following 2 classes.

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

i cannot touch the database to change any table, view or column names.

This works perfectly:

new_orders_last_2_weeks = 
 NewOrder.where("orderdate >= :start and orderdate < :end", 
 {:start => period_start, :end => period_end})

However this:

new_orders_last_2_weeks = 
 NewOrder.joins(:customer).where("orderdate >= :start and orderdate < :end", 
 {:start => period_start, :end => period_end})

generates this wrong sql:

SELECT     `viewNewOrders`.* FROM       `viewNewOrders`  INNER JOIN `customer` ON `customer`.`customerId` IS NULL WHERE     (orderdate >= '2010-09-02' and orderdate < '2010-09-16')

How can i get it to generate the proper condition in inner join?

Thanks.

A: 

the problem was that ActiveRecord is case sensitive about the table and fields names.

so changing

set_table_name "viewNewOrders"

to

set_table_name "viewneworders"

solved the issue.

memical