This project is archived and is in readonly mode.

#427 ✓wontfix
Ben Sandofsky

Bind Lambdas in :conditions

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

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

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

Comments and changes to this ticket

  • Alex MacCaw

    Alex MacCaw June 16th, 2008 @ 11:42 AM

    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


  • Yaroslav Markin

    Yaroslav Markin June 16th, 2008 @ 01:45 PM

    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

  • Neeraj
  • Pratik

    Pratik July 2nd, 2008 @ 02:40 AM

    • 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 => [' = ?', @@count = @@count + 1]})" and the tests passed.


  • Pratik

    Pratik August 3rd, 2008 @ 02:59 PM

    • State changed from “incomplete” to “wontfix”

    Closing till further progress.

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href=""></a>