2

我有一个数组@user.photos,它将返回如下内容:

[#<Photo id: 53, photo_file_name: "Freedom-Tower", 
photo_content_type: "image/jpeg", photo_file_size: 1937702, 
photo_updated_at: "2013-04-23 23:57:22", user_id: 1, 
created_at: "2013-04-23 23:57:23", updated_at: "2013-04-23 23:57:23">, 
#<Photo id: 52, photo_file_name: "sunset-Courtsey",
photo_content_type: "image/jpeg", photo_file_size: 1937702, 
photo_updated_at: "2013-04-23 23:51:22", user_id: 1, 
created_at: "2013-04-23 23:51:24", updated_at: "2013-04-23 23:51:24">

我想创建一个条件来检查用户是否最近创建了一张照片

像:@user.photos.any?{ |x| x.created_at btw Time.now..-5 秒}??

4

2 回答 2

8

您可以使用 5.seconds.ago:

@user.photos.any? {|x| x.created_at > 5.seconds.ago }
于 2013-04-25T21:54:30.533 回答
3

怎么样:

@user.photos.any? { |x| x.created_at > (Time.now-5.seconds) }

上限有原因吗?将来可以创建照片吗?

于 2013-04-25T21:54:24.193 回答