activerecord-hackery / ransack

Object-based searching.
https://activerecord-hackery.github.io/ransack/
MIT License
5.65k stars 790 forks source link

Question: is there a way to test individual records if they comply with search params? #1268

Open mareczek opened 2 years ago

mareczek commented 2 years ago

Ransack is THE MOST valuable gem ever. It's absolutely great - thank you for creating and maintaining it!

It would be awesome if there was a method for testing individual records if they comply with the search params.

Having search params defined as:

search_params: {
  name_cont: 'hello'
}

we use it as: Client.ransack(search_params).result

which yields a beautiful sql query with results.

I would love to be able to perform a test on a individual record without touching the database as such: client.ransack_test(search_params) == true

Is something like the above possible?

slimdave commented 2 years ago

Late to this issue, but while I don't think it's feasible to do this without touching the database, we do something similar by chaining on a call to #exists?.

irb(main):006:0> Book.ransack(title_cont: "phys").result.exists?(id: 186000)
=> true
irb(main):007:0> Book.ransack(title_cont: "phys").result.exists?(id: 186001)
=> false

This is generally going to be very efficient because the RDBMS is likely to rewrite the query execution plan.