In Ransack, how to write a regular ransacker to search by the number of related models

(This is a more specific version of question , which I asked about earlier, but did not receive an answer, I hope that this does not contradict the SO rules)

Given that I have a Rails 4 application with a model Sponsorand a model Sponsorship, so that a Sponsorhas a lot sponsorshipsand a Sponsorshipbelongs to a Sponsor, I want to find sponsors with several sponsorship services that are greater than, equal to or less than a certain amount. For this, I'm trying to use Ransack, the search stone that the application uses.

Here is the command that in the rails console does what I need (in this particular case, it returns sponsors who have more than 1 sponsorship):

Sponsor.joins(:sponsorships).group("sponsors.id").having("count(sponsorships.id) > ?",1)  

Could you help me create ransacker from this command so that I can use Ransack to perform this function?

(I was told that my problem was better solved using a completely different approach using count_cache in Rails, but still I would be pleased to see this with Ransack if there is nothing more useful than this.)

+4
source share

Source: https://habr.com/ru/post/1569246/


All Articles