views:

38

answers:

2

This is an extension of an earlier question. I realized, what I really want is to go to a URL /report/thisweek and it will do a .find (or named_scope) across contact_emails.date_sent where date_sent is between MONDAY and FRIDAY of the week to which Date.today belongs.

In other words, if today is THURSDAY, it will do a search for all emails MONDAY through THURSDAY of this week.

Not sure if this is doable or makes sense, but I think that's what I'd ultimately am trying to do.

Thanks!

EDIT: This is what I tried and I get an undefined local variable error for 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
A: 

You could do something along the lines of this:

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

Basically, by using ActiveSupport::CoreExtensions::Time::Calculations you will get new methods for time manipulation.

Pran
so basically I could use emails = contact_emails.this_week and emails would be a collection of all contact_emails (emails actually sent) with the date within the week? Do I need to do anyting to use the ActiveSupport extensions?
Angela
You may have to alter what I wrote and adapt it for your purposes. ...moreover, I haven't tested it... so there might be syntax errors :)This gets the collection of ContactEmails with the condition that the date_sent field is between the current date and the date the at the beginning of the week (which equates to Monday, as per the documentation). Furthermore, remember that named_scope applied to activerecord models, so you would have to use it like 'ContactEmail.this_week'. You don't need to do anything, Rails requires ActiveSupport by defaults.
Pran
A: 
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

I hope this will help you.

Fer
I made this test on sunday to clarify the point of the weekend:>> 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
Fer
does :date_sent => work_days basically mean monday_000 < :date_sent < friday_2359? this is interesting syntax for conditions, I haven't seen this, I definitely want to check it out...where can I read more?
Angela
Angela: go to [ http://api.rubyonrails.org/classes/ActiveRecord/Base.html]and seach for "range" with your browser, the second one says:«A range may be used in the hash to use the SQL BETWEEN operator: Student.find(:all, :conditions => { :grade => 9..12 })»
Fer