2014-02-17 59 views
-2

我有一個Model屬性的Model Model。 我想創建一個查詢,返回員工不在假期。用滑軌查詢

例如:

employees = Employee.where("disease = ? AND (Date.today < holidays.starts_at OR Date.today > holidays.ends_at)", false).order("engagement ASC").all 

員工型號:

class Employee < ActiveRecord::Base 

    has_many :holidays, :dependent => :destroy 

    after_initialize do 
    self.acceso ||= true 
    end 
end 

假期型號有:starts_at, :ends_at, :employee_id屬性

回答

1

你也許可以做到像

current_holidays = Holiday.where(["starts_at <= :now AND ends_at >= :now", {:now => Time.now}]) 
employees_on_holiday = current_holidays.map(&:employee_id) 
employees_not_on_holiday = Employee.where(["NOT id IN (?)", employees_on_holiday]) 

根據您的需求進行調整,並且我確信它也應該可以在單個查詢中完成。