Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9252623
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T10:59:36+00:00 2026-06-18T10:59:36+00:00

I have a method which is meant to return a number of items from

  • 0

I have a method which is meant to return a number of items from the database based on a set of criteria:

scope :expired_not_marked, lambda { |client|
items = where('items.status > 0 AND items.expires_at < ? AND items.expired_at IS NULL AND (winning_bid_id IS NULL OR winner_id IS NULL)', Time.now)
unless client.nil?
    items.where('items.client_id = ?', client.id)
end
}

It’s being called as Item.expired_not_marked nil. When I run this from the IRB I get a lot of results but it shows the SQL query being executed as:

SELECT `items`.* FROM `items` 

It’s pretty obviously that was not the original author’s intent. As a result the same items are being processed over and over.

Why is this broken, and how do I fix it. The where clause seems correct. The above method is within the item.rb model.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T10:59:37+00:00Added an answer on June 18, 2026 at 10:59 am

    Your problem is that your lambda sometimes returns nil and a scope that returns nil won’t do anything useful.

    The lambda will return the value of its last expression. In your case, that expression will be the unless. So if client is not nil, it will return this:

    items.where('items.client_id = ?', client.id)
    

    and all will be good. But if client.nil? is true, the unless will evaluate to nil and your scope will return nil. I think you’d be better off with something like this:

    scope :expired_not_marked, lambda { |client|
      items = where('items.status > 0 AND items.expires_at < ? AND items.expired_at IS NULL AND (winning_bid_id IS NULL OR winner_id IS NULL)', Time.now)
      unless client.nil?
        items = items.where('items.client_id = ?', client.id)
      end
      items
    }
    

    That way you always have a clear, explicit, and well defined return value.


    The ActiveRecord Query Interface Guide recommends that you use class methods for scopes that take arguments:

    Using a class method is the preferred way to accept arguments for scopes.

    so you could also do this if the lambda approach is too noisy:

    def self.expired_not_marked(client)
      items = where('items.status > 0')
      items = items.where('items.expires_at < ?', Time.now)
      items = items.where('items.expired_at IS NULL')
      items = items.where('winning_bid_id IS NULL OR winner_id IS NULL')
      unless client.nil?
        items = items.where('items.client_id = ?', client.id)
      end
      items
    }
    

    You don’t have to use a class method of course. And you don’t have to break the query into a bunch of little where calls for each component but it might be easier to read this way.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Dear iPhone Developers, I have an instance method which is meant to return a
I have the below method which is meant to append information to a file
I have a class cnList which stores a flexible number of items. This list
I have a number of search functions (stored procedures) which need to return results
Imagine you have a large number of records inside an XQuery-based XML database: <widgets>
Suppose I have a method to calculate combinations of r items from n items:
I have this method which lets me translate the position of an object, animating
I have this method which leaks ~ 6KB : + (EInspectorFacilityInfo*) newWithNode: (CXMLNode*) node
I have a method which returns single word as a String. I need to
I have a method which takes a list and do some processing on it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.