1

I need to do a scope where I find all files that have certain extensions - my current non-functional attempt is this:

scope :visual, where(%w[.gif .jpg .jpeg .tif .tiff].include? File.extname(item_file_name), true)

This attempt gives me errors - how do you create scopes where the conditions are not just a straight SQL query?

4

1 回答 1

0

您需要直接对这些标准进行编码。

scope :visual, where("RIGHT(item_file_name,3) IN (?)
                      OR RIGHT(item_file_name,4) IN (?)",
               ['gif', 'jpg', 'tif'],
               ['jpeg', 'tiff'])

我建议将文件扩展名或 MIME 类型存储为自己的列,然后进行查询。它将比使用字符串函数更高效。实现本身将是相似的,但更简单。

于 2011-05-13T04:08:10.640 回答