2010-05-08 2 views
0

이것은 이전 질문의 연장입니다. 실현하고자하는 것은 실제로 URL /report/thisweek으로 이동하여 contact_emails.date_sent에 걸쳐 .find (또는 named_scope)을 수행하는 것입니다. 여기에서 date_sentDate.today이 속하는 주 중 MONDAY와 FRIDAY 사이에 있습니다.Ruby on Rails에서 월요일 - 금요일을 의미하는 "이번 주"로 검색 범위를 어떻게 정의 할 수 있습니까?

즉, 오늘이 목요일 인 경우 이번 주 중 월요일부터 목요일까지 모든 이메일을 검색합니다.

이것이 가능하거나 이해할 수 있는지 확실하지 않지만 궁극적으로 그렇게하려고 생각합니다.

감사합니다.

편집 : 이것은 내가 뭘하려하고 나는 work_days

class ContactEmail < ActiveRecord::Base 
    attr_accessible :contact_id, :email_id, :status, :subject, :body, :date_created, :date_sent 

    belongs_to :contact 
    belongs_to :email 
    belongs_to :user 

    named_scope :this_week, :conditions => {:date_sent => work_days} 

    protected 
    # Returns a time range corresponding to work days of current or given time's week 
    def work_days(t=Time.now) 
     monday_0000 = t.at_beginning_of_week 
     friday_2359 = 5.days.since(monday_0000)-1.second 
     (monday_0000 .. friday_2359) 
    end 

end 

답변

0
YourModel < ActiveRecord::Base  
    named_scope :this_week, :conditions => {:date_sent => work_days} 

(...) 

    protected 
    # Returns a time range corresponding to workdays of current or given time's week 
    def work_days(t=Time.now) 
     monday_0000 = t.at_beginning_of_week 
     friday_2359 = 5.days.since(monday_0000)-1.second 
     (monday_0000 .. friday_2359) 
    end 
end 

이 정보가 도움이되기를 바랍니다.

+0

주말을 명확히하기 위해 일요일에이 테스트를 실시했습니다. >> Time.now => Sun May 09 12:43:58 +0200 2010 >> work_days => Mon May 03 00:00:00 +0200 2010..Fri May 07 23:59:59 +0200 2010 – Oinak

+0

않습니다 : date_sent => work_days는 기본적으로 monday_000을 의미합니다 <: date_sent Angela

+0

Angela : [http://api.rubyonrails.org/classes/ActiveRecord/Base.html] 으로 이동하고 브라우저에서 "범위"를 검색하면 두 번째 것은 « 범위에서 사용할 수 있습니다. SQL BETWEEN 연산자를 사용하는 해시 : Student.find (: all, : conditions => {: grade => 9..12}) » – Oinak

1

당신이의 라인을 따라 뭔가 할 수에 대한 정의되지 않은 지역 변수 오류 얻을 : ActiveSupport::CoreExtensions::Time::Calculations를 사용하여, 기본적으로

named_scope :this_week, :conditions => ['date_sent > ? and date_sent < ?', Time.now.at_beginning_of_week, Time.now] 

을 당신은 시간 조작을위한 새로운 방법을 얻을 것입니다.

+0

기본적으로 전자 메일 = contact_emails.this_week을 사용할 수 있으며 전자 메일은 일주일 이내에 모든 contact_emails (실제로 보낸 전자 메일) 컬렉션입니다. ActiveSupport 확장 프로그램을 사용하려면 아무쪼록해야합니까? – Angela

+0

내가 작성한 내용을 변경하고 용도에 맞게 수정해야 할 수도 있습니다. ... 또한 테스트하지 않았으므로 구문 오류가있을 수 있습니다. :) 이것은 date_sent 필드가 현재 날짜와 시작 날짜 사이의 날짜 인 ContactEmails 컬렉션을 가져옵니다. 주 (문서 당 월요일에 해당). 또한 named_scope는 activerecord 모델에 적용되므로 'ContactEmail.this_week'와 같이 사용해야합니다. 아무 것도 할 필요가 없으며, 레일스는 기본적으로 ActiveSupport가 필요합니다. – Pran

관련 문제