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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T16:18:47+00:00 2026-05-22T16:18:47+00:00

I have a fairly straightforward query that I can’t seem to get right…. My

  • 0

I have a fairly straightforward query that I can’t seem to get right….

My model:

  • User – has many Friendships (with other users)
  • User – has many submissions
  • User – has many comments
  • User – has many votes

I need a count that represents

All current_user’s friends, whose submissions, comments or votes created_at dates are > current_user.last_refresh_date

Right now, I am building up an array by iterating over friendships and adding all submissions, comment and votes. I then re-iterate this built-up array while comparing the dates to determine if the count should be incremented. Not the most ideal solution.

Edit:

@TobiasCohen

Efficient solution. Thanks!

Followup:

I wish to add yet one more count to the present query. I need to count all new comments & votes on the current_user.submissions that are not part of the original count (ie. not a friend).

Psuedo-code :

current_user.submissions.join(:comments, :votes, :friends).where(‘last_activity >? AND friend_id != ?’, current_user.last_refresh_date, current_user.id).count

I can’t quite get the query correct (new to complex queries via active record).

I was going to make it a separate query and then add it to the original count. Can it be absorbed into one query instead of two?

  • 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-22T16:18:48+00:00Added an answer on May 22, 2026 at 4:18 pm

    I think you’d get the best results by adding a cache column on User, let’s call it :last_activity_at, then update this with an after_create callback on Submission, Comment and Vote.

    class Submission < ActiveRecord::Base
      belongs_to :user
    
      after_create :update_user_last_activity_at
    
      private
        def update_user_last_activity_at
          user.update_attribute :last_activity_at, Time.now
        end
    end
    

    You could then fetch users simply with:

    current_user.friends.where('last_activity_at > ?', current_user.last_refresh_date)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a fairly straightforward question in Rails 3 to which I can't seem
I have a fairly straightforward WCF service that performs one-way file synchronization for a
Fairly straightforward question. I have a map that I wish to initialize by calling
I have a fairly straightforward page ( http://www.kwatee.net ) that renders ok with Firefox,
We use a fairly straightforward naming standard for indexes that I can validate programmatically,
I have a layout that should be fairly straightforward, but for some unknown reason
This is probably fairly straightforward but i can't seem to find a reasonable explanation
I have a custom view that has to track the user's location. I put
I have a fairly straightforward set of php forms that take users through a
I have a fairly straightforward data model for a Core Data app. Basically, I'm

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.