Mark Locklear
Mark Locklear

Reputation: 5325

Rails paper clip search file name

I have a requirement for users to be able to search file names uploaded by paperclip. Looking at an uploaded document looks like this...

2.0.0-p353 :002 > d=Document.last
  Document Load (0.4ms)  SELECT  "documents".* FROM "documents"   ORDER BY "documents"."id" DESC LIMIT 1
 => #<Document id: 5, name: "hampton_george_27_70", transcription: "Asheville Nov 8th 1853 received of James A. Patton...", created_at: "2014-08-22 16:10:19", updated_at: "2014-08-22 16:37:51", document_file_name: "hampton_george_27_70.jpeg", document_content_type: "image/jpeg", document_file_size: 519652, document_updated_at: "2014-08-22 16:10:19"> 
2.0.0-p353 :003 > d.document_file_name
 => "hampton_george_27_70.jpeg" 

But these are no traditional rails type models, so how would I go about searching them?

Upvotes: 0

Views: 137

Answers (1)

infused
infused

Reputation: 24337

Document appears to be a model, so you can query it. For example, to search for documents with "george" in the file name:

search_term = "george"
documents = Document.where(['document_file_name LIKE ?', "%#{search_term}%"])

Upvotes: 3

Related Questions