2014-05-21 98 views
3

我更新遺留的Rails 2.0的應用程序,發現這個代碼,這似乎並不爲Rails 4工作了...:多個條件4

company.rb:

has_many :orders 
has_many :pending_orders, 
    :class_name => "Order", 
    :conditions => "(orders.coworker_id IS NULL OR orders.delivery_time IS NULL) AND orders.finished_at IS NULL", 
    :order => "orders.created_at ASC" 

在這種情況下,文檔不是很全面,所以我正在尋找正確的解決方案來將此代碼更新到Rails 4語法。這是我的嘗試,但我不知道這是否是正確的解決方案:

has_many :pending_orders, -> { (where(orders: {coworker_id: nil}) || where(orders: {delivery_time: nil})) && where(orders: {finished_at: nil}) }, class_name: "Order" 
+0

它有效或沒有? – Pavan

+0

它不顯示錯誤,但我不確定是否獲取正確的數據。我現在正在測試... – crispychicken

回答

1

如果您嘗試不起作用,嘗試給這樣

has_many :pending_orders, -> { where(orders.coworker_id IS NULL OR orders.delivery_time IS NULL) And orders.finished_at IS NULL }, class_name: "Order" 

Source

+0

當我嘗試將And或Or連鎖到多個條件時,我會收到「意外的tCONSTANT,期待'}'」。 – JosephK