Use `pg_search` to search an Active Storage attachment

33 views Asked by At

I have a model with has_one_attached :file, and the following pg_search scope to search the filename of that attachment:

  pg_search_scope :search_filename, associated_against: {
    file_blob: :filename
  }

This works correctly. However, if I also want to order by an attribute of the blob, I get an error.

For example, this works (although I've just noticed the order doesn't seem to do anything:

Model.includes(:file_blob).search_filename("foo").order("created_at")

Whereas this:

Model.includes(:file_blob).search_filename("foo").order("active_storage_blobs.filename")

Gives the error:

ActionView::Template::Error (PG::UndefinedTable: ERROR:  missing FROM-clause entry for table "active_storage_blobs"
LINE 1: ...98a4265.rank DESC, "my_models"."id" ASC, active_sto...
                                                             ^
):

How do I fix these problems?

0

There are 0 answers