2011-03-25 47 views
0

我的任務屬於不同型號,但始終分配給公司和/或用戶。我試圖縮小顯示的內容,通過那裏的due_at日期將它們分組,而不需要做太多的查詢。Rails 3顯示部分任務

有一個應用助手

def current_tasks 
    if user_signed_in? && !current_company.blank? 
    @tasks = Task.where("assigned_company = ? OR assigned_to = ?", current_company, current_user) 
    @current_tasks = @tasks 
    else 
    @current_tasks = nil 
    end 
end 

然後在我的主視圖我有

<%= render :partial => "common/tasks_show", :locals => { :tasks => current_tasks }%> 

我的問題是,在我的任務類我有你看到下面的東西。我有一個名爲due_today的示波器。當我嘗試current_tasks.due_today它的工作原理,如果我嘗試current_tasks.select_due_today我得到一個undefined method "select_due_tomorrow" for #<ActiveRecord::Relation:0x66a7ee8>

def select_due_today 
    self.to_a.select{|task|task.due_at < Time.now.midnight || !task.due_at.blank?} 
end 
+0

哪裏是'選擇適當tomorrow'定義是什麼它的代碼? – 2011-03-25 17:26:00

+0

對不起,它在我的課Task模型中。試圖使用代碼不再查詢sql。我已經在current_tasks中完成了所有任務。現在試圖縮小顯示在哪裏。 – pcasa 2011-03-25 17:27:46

+0

你必須在你的問題之外留下一些東西,因爲給你所有的東西絕對沒有任何理由,當你執行'current_tasks.select_due_today'時'select_due_tomorrow'應該被調用。你的'current_tasks'幫手也違反了MVC。 – 2011-03-25 17:32:26

回答

0

如果你想調用current_tasks.select_due_today那麼它必須是一個類的方法,這樣的事情(翻譯你的Ruby到SQL):

def self.select_due_today 
    select('due_at < ? OR due_at IS NOT NULL', Time.now.midnight) 
end 

或者,你可以有幾乎同樣的事情,作爲一個範圍 - 但把它放在一個lambda使Time.now.midnight是指呼叫範圍調用,而不是當你定義它。

[編輯切換IS NULLIS NOT NULL - 這反映了問題的紅寶石,但沒有任何意義,因爲這將否定的OR的意思左]

+0

它是這樣命名的範圍。正試圖避免必須對due_today,tomm等進行多次呼叫。 – pcasa 2011-03-25 18:41:41