Rails, Ransack: How to Search Habtm Relationship for "All" Matches Instead of "Any"

Rails, Ransack: How to search HABTM relationship for all matches instead of any

With a custom ransack predicate defined as in my answer to your related question, this should work with a simple change to your markup:

- terms.each do |t|
= check_box_tag 'q[id_has_terms][]', t.id

UPDATE

The :formatter doesn't do what I thought, and seeing as how the Ransack repo makes not a single mention of "subquery," you may not be able to use it for what you're trying to do, after all. All available options seem to be exhausted, so there would be nothing left to do but monkey patch.

Why not just skip ransack and query the "photos" table as you normally would with active record (or even with the Arel query you now have)? You already know the query works. Is there a specific benefit you hoped to reap from using Ransack?

Ransack sort on count of HABTM or HMT associated records

Given you have your entities queried with the above scope, for example your index query always has:

# controller
@search = Theme.ransack(params[:q])
@themes = @search.result(distinct: true).with_quotes_count

Have a try of:

# model
ransacker :quotes_count_sort do
Arel.sql('quotes_count')
end

And use the name of the sort in sort_link?

Ransack association search breaking after upgrading rails

I have resolved the issue by monkey patching ActiveRecord AliasTracker.

See my gh issue for the full explanation.

Check if a record has related records through Ransack. Rails

The answer was pretty simple: questions_id_null: false



Related Topics



Leave a reply



Submit