Save 36% for Black Friday! Learn more

New Discussion

Notifications

You’re not receiving notifications from this thread.

How to use Active Search in Rails Discussion

1
General

THANK YOU so much for doing this video and using Claude for the code. I've embraced Claude fully (my wife it's my new best friend), but it's also great to allow me to spend more time on understanding what's going on.

ActiveSearch looks great, can you talk about how ActiveSearch would or wouldn't play nice with Ransack? (I'm an old ransack guy).

Hey Kevin! Glad the video helped. Claude is a big help for us too.

They work fine side by side because they solve different problems and don't touch each other's code.

Ransack turns params like title_cont or published_at_gteq into SQL WHERE clauses on an Active Record relation. It works well for admin tables, sortable columns and structured filters. But it's still a LIKE query on your database, so there's no relevance ranking, no stemming, and it gets slow on large text columns.

ActiveSearch sends your text to a search index. That can be Postgres tsvector, SQLite FTS5, or Meilisearch, Elasticsearch, Typesense and others. It gets back ranked document ids and then loads the records. You get real full-text search with relevance scores and highlighting, and you can change backends without rewriting your queries.

The one place they don't mix well is chaining a Ransack relation onto a search. ActiveSearch picks the page of hits in the index first, so this doesn't do what you'd expect:

Article.search("rails", scope: Article.ransack(params[:q]).result)

The scope: option only controls how the matched records get loaded. It doesn't filter the search. Anything your Ransack conditions exclude is just dropped from that page (counted in results.dropped), so your pages and counts come out wrong. To filter a search, put those fields in the index and use ActiveSearch's own filters:

Article.search(params[:query])
  .filter(account_id: current_account.id, status: "published")
  .filter(published_at: 1.month.ago..)
  .reject(featured: false)

My rule of thumb:

  • Ransack for admin screens and structured filters on columns, where you don't need text relevance.
  • ActiveSearch for the search box users type into, where relevance matters, with the filters you need indexed next to the text.

One thing ActiveSearch doesn't do yet is aggregations, so no faceted counts like "Rails (42), Hotwire (17)" next to your filters. If you need those, run a SQL GROUP BY alongside the search for now.

You can have both in the same app, even on the same model. Ransack is mature and stable. ActiveSearch is still alpha and its API may change, so keep that in mind if you use it in production.

Join the discussion
Create an account Log in

Learning Ruby on Rails? Join our newsletter.

We won't send you spam. Unsubscribe at any time.