[RUBY] When you want to explicitly write OR or AND with ransack

What is ransack?

Search gem. You can easily implement a search in the column of that table or related table. https://github.com/activerecord-hackery/ransack However, if you want to narrow down by complicated search conditions or conditions other than columns, you need to devise a little.

What I want to do this time

I want to search for some elements with ʻAND and the rest with ʻOR. The query looks like this:

SELECT DISTINCT sample_models.* 
  FROM sample_model 
    WHERE (sample_models.column_1 = '202001' AND sample_models.column_2 = 'Hokuto Matsumura') 
      AND ( sample_models.column_3 = 'Jesse' OR sample_models.column_4 = 'Juri Tanaka' ) 
    ORDER BY sample_models.id ASC

Solution: Generate a hash of search criteria

group = {
          "0" => {
                   m: "and",
                   column_1: "202001", 
                   column_2: "Hokuto Matsumura"
                 }, 
          "1" => {
                   m: "or",
                   column_3: "Jesse", 
                   column_4: "Juri Tanaka"
                 }
        }
#result
SampleModel.search(g: group).result

By devising the hash assigned to group, you can manipulate the conditions of grouping, AND, and OR as you like. Convenient! However, if you know that the readability will be reduced or if you do such irregular things, it will cost you to study, so if you know that the search conditions will be out of ransack from the beginning, do not force it to match ransack and do it yourself. I think it's better to write a query.

Recommended Posts

When you want to explicitly write OR or AND with ransack
docker-compose.yml when you want to keep mysql running with docker
lombok.config when you want to pass @Qualifier to @RequiredArgsConstructor with lombok
How to write when you want to keep line breaks and output while avoiding XSS in Rails
Summary of means when you want to communicate with HTTP on Android
When you want to bind InputStream in JDBI3
When you want to use the method outside
[Ruby] When you want to replace multiple characters
If you want to use Mockito with Kotlin, use mockito-kotlin
I want to transition screens with kotlin and java!
When you want to dynamically replace Annotation in Java8
Command when you want to erase all Docker images and containers cleanly and start over
You also need to specify the host when debugging remotely with Java 9 or later
Code to use when you want to process Json with only standard library in Java
What to do when you launch an application with rails
I want to make a list with kotlin and java!
I want to make a function with kotlin and java!
How to write and notes when migrating from VB to JAVA
When you want to implement Java library testing in Spock with multi-module in Gradle in Android Studio 3
When you want to change the MySQL password of docker-compose
I want to implement various functions with kotlin and java!
Delegate is convenient to use when you want to reuse parts
Write DiscordBot to Spreadsheets Write in Ruby and run with Docker
[For super beginners] The minimum knowledge you want to keep in mind with hashes and symbols
A trick when you want to insert a lot of line breaks and tabs when substituting a character string
I want to be able to think and write regular expressions myself. ..
I want to return to the previous screen with kotlin and java!
I want to avoid OutOfMemory when outputting large files with POI
When you want to ZIP download the image data saved locally
[jOOQ] How to CASE WHEN in the WHERE / AND / OR clause
[Java] How to write when passing two or more arguments to super
[Docker] Magic command when you want to wipe out none image
How to write when installing Amazon Corretto 8 on CentOS 8 with Ansible.
When you have introduced devise but want to add more columns
[Rails] I want to add data to Params when transitioning with link_to
I want to perform asynchronous processing and periodic execution with Rail !!!
Practice to use when you want to execute different processing groups serially
Vuze plugin to write and enjoy
When you want to add a string type column with a limited length with the `rails generate migration` command
The first thing to do when you want to be happy with Heroku on GitHub with Eclipse in Java
A memo when you want to clear the time part of the calendar
When you want Rails to disable a session for a specific controller only
I want to display images with REST Controller of Java and Spring!
[For beginners] When you want to say that the JVM (-D) option does not work with the java -jar command, or that the library is buggy.
(Limited to Java 7 or later) I want you to compare objects in Objects.equals
If you want to make a zip file with Ruby, it's rubyzip.
I want to write a nice build.gradle
If you dare to compare Integer with "==" ...
I want to use DBViewer with Eclipse 2018-12! !!
I want to write a unit test!
Basic Rails commands you want to learn
[Rails] [Memo] When to add = to <%%> and when not
Create an or search function with Ransack.
How to write and explain Dockerfile, docker-compose
Convert JSON to TSV and TSV to JSON with Ruby
What to do when you want to delete a migration file that is "NO FILE"
If you are using Android Room and want to change the column definition
When you want to notify an error somewhere when using graphql-spring-boot in Spring Boot
How to get only articles and transactions that you commented or messaged [includes]
Code used when you want to process Json with only the standard library in Java (improved version) gson unnecessary
Summary of copy and paste commands used when you want to delete the cache in iOS application development anyway