Unusual scope in Rails
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 开发者_Go百科do you create scopes where the conditions are not just a straight SQL query?
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.
精彩评论