课程.rb
has_many :current_users, :through => :user_statuses, :source => :user, :conditions => ['user_statuses.updated_at > ?', 1.hour.ago]
安慰
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
>>
请注意,在检查1.hour.ago
条件时,尽管我提出请求的时间相差 30 秒,但它使用相同的时间作为起点。退出控制台并重新启动它会清除它,但它会在新的时间再次发生。这种行为存在于测试和浏览器中。如何让模型使用基于时间的条件进行has_many :through
查找?