检查数组中的元素是否在最近5秒内创建了create_at

时间:2013-04-25 21:49:47

标签: ruby-on-rails ruby arrays

我有一个@ 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秒} ??

2 个答案:

答案 0 :(得分:8)

您可以使用5.seconds.ago:

@user.photos.any? {|x| x.created_at > 5.seconds.ago }

答案 1 :(得分:3)

怎么样:

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

上限是否有原因?可以在将来制作照片吗?