문제

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?

도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top