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

  • Home
  • SEARCH
  • 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 7905409
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T10:27:57+00:00 2026-06-03T10:27:57+00:00

course.rb has_many :current_users, :through => :user_statuses, :source => :user, :conditions => [‘user_statuses.updated_at > ?’,

  • 0

course.rb

  has_many :current_users, :through => :user_statuses, :source => :user, :conditions => ['user_statuses.updated_at > ?', 1.hour.ago]

console

Loading development environment (Rails 3.2.2)
>> course = Course.find(1)
  Course Load (0.3ms)  SELECT `courses`.* FROM `courses` WHERE `courses`.`id` = 1 LIMIT 1
=> #<Course id: 1, title: "Course 1", created_at: "2012-04-17 19:17:15", updated_at: "2012-04-17 19:17:15">
>> Time.now
=> 2012-04-23 08:29:45 -0400
>> course.current_users.count
   (0.4ms)  SELECT COUNT(*) FROM `users` INNER JOIN `user_statuses` ON `users`.`id` = `user_statuses`.`user_id` WHERE `user_statuses`.`user_id` = 1 AND (user_statuses.updated_at > '2012-04-23 12:28:40')
=> 0
>> Time.now
=> 2012-04-23 08:30:07 -0400
>> course.current_users.count
   (0.4ms)  SELECT COUNT(*) FROM `users` INNER JOIN `user_statuses` ON `users`.`id` = `user_statuses`.`user_id` WHERE `user_statuses`.`user_id` = 1 AND (user_statuses.updated_at > '2012-04-23 12:28:40')
=> 0
>>

Notice when checking the 1.hour.ago condition it uses the same time as a starting point despite the 30 second difference between the times when I made the request. Exiting console and restarting it clears it out, but it happens again with a new time. This behavior exists in testing and a browser as well. How do I get a model to use a time based condition for a has_many :through find?

  • 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-03T10:27:58+00:00Added an answer on June 3, 2026 at 10:27 am

    Putting the query in the model at all didn’t work, either in a has_many :through setup or in a method. So I ended up removing the association and putting the query in the controller. This allows the current time to be calculated when the request is made.

    model:

    has_many :user_statuses
    

    controller:

    @course = Course.find(params[:id])
    @current_users = @course.user_statuses.where('updated_at > ?', 1.hour.ago)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a fairly standard 'has_many :x, :through => :y' relationship with a user,
Imagine I have a model called Course and each course has_many Modules . However
I have the following tables: class FinalExam < ActiveRecord::Base belongs_to :course has_many :pages, :as
I am trying to write a function completed_steps_for_course(course) in my User model that finds
Take the following simple object model for example: class Course has_many :enrollments has_many :students,
I have a model called websites that has_many :likes , and of course another
so I have two models: class User < ActiveRecord::Base has_and_belongs_to_many :followed_courses, :class_name => Course
I have 4 models, Message, Group, User, Membership class Group < ActiveRecord::Base has_many :memberships
A Course has many Lessons , and they are chosen by the user with
I have the following: User has_many Listings Listing has_many Offers Pretty basic. A user

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.