0218fde3a78fadbadb566bdb40d7b0dd

So my User has an association proxy for her expenses. spent_at is a Date field in the DB. The day being passed in is a Ruby Date. Mainly, I want to be sure hashed conditions are safe from hacks. Also wondering if there is a more convenient way to do beginning_of and end_of calcs on Date objects.

NOTE: At Rails 1.2.5, so no end_of_week on Time. I think that's available in edge.

1
2
3
4
5
has_many :expenses, :dependent => :destroy do
  def for_week(day)
    find(:all, :conditions => { :spent_at => day.to_time.beginning_of_week.to_date..(day.to_time.beginning_of_week.to_date + 6)})
  end
end

Refactorings

No refactoring yet !

143c67bc66364c543e85bc907717a8ab

Max Lapshin

November 19, 2007, November 19, 2007 10:03, permalink

No rating. Login to rate!

perhaps use to_s(:db)

1
2
3
4
5
6
7
class Time 
  def to_week_range
   (beginning_of_week..((self+7.days).beginning_of_week))
  end
end

Article.find(:all, :conditions => "published_at #{day.to_time.to_week_range.to_s(:db)}")

Your refactoring





Format Copy from initial code

or Cancel