Question

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?

Was it helpful?

Solution

You need to code these criteria directly.

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

I recommend storing the file extension or MIME type as its own column, and querying against that. It will be more performant than using string functions. The implementation itself would be similar, but simpler.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top