Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

Bind Lambdas in :conditions

#427

Let's say you want to find records based on the current time.

def find_within_last_week(*args)
  with_scope(:find => {:conditions => ['assignments.created_at > ?', 7.days.ago.to_s(:db)} do
    find(*args)
  end
end

Except, that time statement will be evaluated when the class is first loaded, which quickly makes it useless. The less-than-documented solution used to be

['assignments.created_at > ?', '#{7.days.ago.to_s(:db)']

But that's just silly when ruby has lambdas. With this patch, you can write:

def find_within_last_week(*args)
  with_scope(:find => {:conditions => ['assignments.created_at > ?', lambda {7.days.ago.to_s(:db)}} do
    find(*args)
  end
end

Reported by Ben Sandofsky · June 16th, 2008 @ 10:44 AM

State: wontfix
Milestone: none
Assigned to: Pratik Pratik
Importance: none

Activity

  1. Alex MacCaw
    Alex MacCaw

    At the moment you can do:

    named_scope :last_week, lambda { ({:conditions => ['assignments.created_at > ?', 7.days.ago.to_s(:db)]}) }

    I agree, the normal find should have the same functionality

    +1

    June 16th, 2008 @ 11:42 AM

  2. Yaroslav Markin
    Yaroslav Markin

    I am SO +1 on this. As Alex mentioned, named_scope already has this.

    Probably it will be possible to merge this stuff on the way to 3.0

    June 16th, 2008 @ 01:45 PM

  3. Neeraj
  4. Pratik
    Pratik
    • State changed from new to incomplete
    • Tag set to activerecord, patch, verified
    • Assigned user set to Pratik

    Can you please add a test case which will fail for non-lambda version. I tried changing test included in the patch to use "with_scope(:find => {:conditions => ['companies.id = ?', @@count = @@count + 1]})" and the tests passed.

    Thanks.

    July 2nd, 2008 @ 02:40 AM

  5. Pratik
    Pratik
    • State changed from incomplete to wontfix

    Closing till further progress.

    August 3rd, 2008 @ 02:59 PM