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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T22:31:11+00:00 2026-05-20T22:31:11+00:00

I have a model named Post (blog post) and a model named Category .

  • 0

I have a model named Post (blog post) and a model named Category. Each post belongs_to a category. Each category has an attribute named retainer that specifies the amount of time before a post “expires”, so for example movies_category.retainer = 30.days

What I’m trying to do is create a scope for Post that finds all of the posts which are “expired”. So for example, assuming I were to hardcode the value of 30.days and it were to apply to all categories (therefore all posts), the scope would be:

scope :expired, lambda { where("posts.created_at < ?", 30.days.ago) }

However, instead of hardcoding the value 30.days.ago, I want to get the retainer value from the post’s category and base the condition on that, so something like:

scope :expired, lambda { where("posts.created_at < ?", 
  Time.now - post.category.retainer) }

So put into words: I want to get all of the posts which are expired, and the expiration status is determined by each post’s category’s retainer value (i.e. posts in the movies category expire in 10 days, posts in the games category expire in 5 days, etc.)

Is this even possible? Would I require some form of join or something?

  • 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-20T22:31:12+00:00Added an answer on May 20, 2026 at 10:31 pm
    scope :expired, lambda { |retainer| where("posts.created_at < ?", 
      Time.now - retainer) }
    

    Then just use it like:

    Post.expired(post.category.retainer)
    

    Or from the category model like:

    def Posts
      Post.expired(retainer)
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a controller/model hypothetically named Pets. Pets has the following declarations: belongs_to :owner
Suppose I have a Django app named blog. There's a model called Post and
Just starting with FactoryGirl. I have a Model named Subscription . It has a
I have a Model that named word. this is my word model public class
I have one grails application.In that I have one model class named Book. From
I have a Blog model, a Post model and a Comment model: class Blog(models.Model):
Let's say I have a model that looks like this: public class Blog {
My blog post model has a many-to-many field for tags: tags = models.ManyToManyField(PostTag) But
Hello i'm working on a blog app and I have a model: class Post(models.Model):
I have the following model and url routes. There is one Post model that

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.