简体   繁体   中英

Passing multiple fields values to ransack

I have Rails app with a table and I want to have search fields on top of each column which should perform search on the column and those searches should work together. So I'm looking for a way to implement it and stumbled upon Ransack gem which looks nice with all it's selecting and sorting functionality but I can't understand if I can use it to solve my problem.

From the documentation I see that I can pass a parameter to it like this User.ransack(first_name_cont: 'Rya').result.to_sql

But I think it's not enough for my problem and I need something like User.ransack(first_name_cont: 'Foo', last_name_eq: 'Bar', middle_name_in: [Moo, Boo]).result.to_sql

Can I do something like this with Ransack? Or how else can I solve my problem with it or maybe I should use something else?

Ransack takes responsibility of searching on each field to himself. So you specify any field, which exist in your table, in search.

You can combine any field with predicates. For example you have field age :

 User.ransack(age_eq: 21) # => age = 21
 User.ransack(age_lt: 21) # => age < 21
 User.ransack(age_in: [21]) # => age in [21]

Combine with another field 'name':

User.ransack(age_lt: 21, name_eq: 'Tim') # => age < 21 AND name = 'Tim'
User.ransack(age_gteq: 21, name_eq: 'Tom') # => age >= 21 AND name = 'Tom'

You can pass multiple values by using in predicate or not_in for opposite operation.

And more, see basic predicates with description .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM