我有一个调用 find_photos 方法的控制器,向它传递一个查询字符串(文件名)
class BrandingPhoto < ActiveRecord::Base
def self.find_photos(query)
require "find"
found_photos = []
Find.find("u_photos/photo_browse/photos/") do |img_path|
# break off just the filename from full path
img = img_path.split('/').last
if query.blank? || query.empty?
# if query is blank, they submitted the form with browse all- return all photos
found_photos << img
else
# otherwise see if the file includes their query and return it
found_photos << img if img.include?(query)
end
end
found_photos.empty? ? "no results found" : found_photos
end
end
这只是搜索一个满是照片的目录——没有表支持它。
理想情况下,我希望能够将 find_photos 返回的结果数量限制在 10-15 左右,然后根据需要获取接下来的 10-15 个结果。
我在想执行此操作的代码可能涉及循环 10 次并获取这些文件 - 将最后一个文件名存储在变量中或作为参数,然后将该变量发送回方法,告诉它继续搜索文件名。
这假定文件每次都以相同的顺序循环,并且没有更简单的方法可以完成此操作。
如果有任何建议,我很想听听他们/看一些你如何实现这一点的例子。
谢谢你。