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 3984486
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T05:46:33+00:00 2026-05-20T05:46:33+00:00

Hey guys. I have a model, Item , which is set to expire in

  • 0

Hey guys. I have a model, Item, which is set to “expire” in created_at + 100.days. I am trying to create a scope that will return the items which will expire in a supplied time, such as 10.days.

So far I have this:

scope :expires_within, lambda {|time| where("created_at > ?", time)}

So that I could eventually do something like:

Item.expires_within(10.days)

Which would return a collection of items which expire within 10 days.

This isn’t right, I just wanted to have a skeleton of what I should have. I can’t figure out if it’s possible to do this through a scope or not. It seems to me like I would be able to if I had a column in the table called expires_at, but the problem is that I would rather not create such a column because I want the expiration time, above stated as created_at + 100.days, to be flexible.

I just wanted to see if it would be possible without such a column, or if it can be done but not with a scope, I’d like to hear that possibility as well. If not, I’ll go ahead and add it.

  • 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-05-20T05:46:34+00:00Added an answer on May 20, 2026 at 5:46 am

    I have come up with a solution I believe. I will continue to test it.

    scope :expires_within, lambda {|time| where("created_at < ?", time.from_now - 100.days)}
    

    Basically my original formula to calculate whether something was expired was:

    Time.now > created_at + 100.days
    

    Since I want to determine whether something expires within a future time (e.g. 10.days), I add it to the left-hand side:

    Time.now + time > created_at + 100.days
    

    Remember, 100.days is how long an item has to live before it expires.

    Finally, I solve for created_at so that it’s by itself, since that’s what the sql query demands, by subtracting 100.days from the other side:

    created_at < (Time.now + time) - 100.days
    

    This could be made more concise, as Ryan pointed out, by doing:

    created_at < time.from_now - 100.days
    

    Basic math I guess. I’m still pretty dazed from thinking about it too hard, so I will test it a bit to make sure it works as intended. If it does, Item.expires_within(10.days) should return a collection of items which will expire within 10 days (or before).

    EDIT: I’ve refactored this to ignore the items which have already expired. So the new scope looks for the items that have not yet expired and will expire within a specified time frame:

    scope :expires_within, lambda {|time| where("created_at BETWEEN ? AND ?", 100.days.ago, time.from_now - 100.days)}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hey guys, I am trying to have a theme set up and be able
Hey guys i have a question here, How do i create a body that
Hey guys I have an admin page that checks if you are admin before
Hey guys I have one more question lol. I am using a script that
Hey guys I have a query that currently finds the latest comment for each
Hey guys i have a question which i have been looking for the answer
Hey guys I got a weird issue.. I have a datalist that's only loaded
Hey Guys I have four integer array lists which were created after parsing a
Hey guys I have a query that selects data and organizes but not in
Hey guys I have an ajax jquery function that receives data from a php

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.