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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T07:31:39+00:00 2026-06-02T07:31:39+00:00

I have the following relationships: Category has_many :posts Post has_many :comments Post has_many :commenters,

  • 0

I have the following relationships:

Category has_many :posts
Post has_many :comments
Post has_many :commenters, :through => :comments

I have the following eager load, giving me posts, comments and commenters (note that I need all 3, and hence the includes as opposed to joins)

category.posts.includes(:comments, :commenters)

However, I’d like to limit comments (and if possible commenters) to only those created in the past two weeks while still returning the same set of posts. Initially I thought I could specify a condition on the includes:

category.posts.includes(:comments, :commenters).where("comments.created_at > ?", 2.weeks.ago)

But found that this returns only the posts that meet the condition. I’m thinking that I may need to do something like performing a subquery on comments and then performing a join. Is there an easy way to do this with AR of would I be better off doing this with sql?

  • 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-02T07:31:41+00:00Added an answer on June 2, 2026 at 7:31 am

    Finally managed to figure this out from reading this page:
    http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html

    I simply needed to create an association in my Post model like:

    Post has_many :recent_comments, :class_name = 'Comment', :conditions => ["created_at > ?", 2.weeks.ago]
    

    Then I could do the following to get the desired ActiveRecord::Association object:

    category.posts.includes(:recent_comments => :commenters)
    

    There was also a suggestion of doing this by using a scope on a model. However, I read somewhere (I think it was on SO) that scopes are on their way out and that ARel has taken their place so I decided to do this without scopes.

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

Sidebar

Related Questions

I'm following this tutorial which is working splendidly for has_many :through relationships. I've got
I have three tables that have the following relationships between them: Account { public
I have the following relationships set up: A HABTM B B belongsTo C C
In my Rails app I'll have the following relationships: class Location < ActiveRecord::Base; end
I have 3 objects with the following relationships: Feature contains a List<> of FeatureGroup
Currently I have a database with the following relationships: One Client has multiple Entities
I have the following relationships setup: class Article < ActiveRecord::Base has_and_belongs_to_many :authors end class
I have the following kinds/relationships in my datastore: UserAccount 1-to-1 PersistentLogin 1-to-many PersistentLogins They
Assume I have the following tables and relationships: Person - Id (PK) - Name
I have a few tables with the following relationships: Company hasMany Jobs, Employees, and

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.