[RUBY] Pass a variable to Scope.

I was a little confused about scope at work, so I will summarize it here as a memorandum. By the way, the person I am writing is a person who has been an engineer for one month. If you have any mistakes, please comment.

What I wanted to do

I want to pass variables for each to scope.

Actual code

Office.rb


  scope :search, lambda { |keyword|
                   where('name LIKE (?) OR
                          address LIKE (?) OR
                          near_station LIKE (?) OR
                          introduction LIKE (?) OR
                          company LIKE (?)',
                          "%#{keyword}%",
                          "%#{keyword}%",
                          "%#{keyword}%",
                          "%#{keyword}%",
                          "%#{keyword}%")
                        }

offices_controller.rb


      keywords = params[:keyword].split(/[[:blank:]]+/).select(&:present?)
      offices_list = []
      keywords.each do |keyword|
        offices_list += Office.search(keyword)

The code is only partially excerpted, but I was able to implement it this way. In the end, it became the above code, but the following code also worked normally.

Office.rb


  scope :search, keyword { where('name LIKE (?) OR
                          address LIKE (?) OR
                          near_station LIKE (?) OR
                          introduction LIKE (?) OR
                          company LIKE (?)',
                          "%#{keyword}%",
                          "%#{keyword}%",
                          "%#{keyword}%",
                          "%#{keyword}%",
                          "%#{keyword}%")
                        }

Recommended Posts

Pass a variable to Scope.
Java variable scope (scope)
Java variable scope
Notation to put a variable in a string
Corresponds to Scope
How to use scope and pass processing (Jakarta)
[Introduction to Java] Variable scope (scope, local variables, instance variables, static variables)
Assign a Java8 lambda expression to a variable and reuse it
How to leave a comment
To write a user-oriented program (1)
Pass parameters to Rails link_to
How to insert a video
How to create a method
[Rails] How to use Scope
Pass C ++ char * to Swift
[Ruby] I want to put an array in a variable. I want to convert to an array
How to store the information entered in textarea in a variable in the method
[Swift5] How to communicate from ViewController to Model and pass a value
How to use scope (JSP & Servlet)
How to add columns to a table
Try to make a simple callback
Preparing to create a Rails application
Android Development-Try to display a dialog-
Send a pull request to GitHub
How to make a Java container
How to sign a Minecraft MOD
How to make a JDBC driver
[Java] How to create a folder
How to write a ternary operator
[Swift] How to send a notification
How to write Java variable declaration
What is a reference type variable?
How to make a splash screen
How to make a Jenkins plugin
Assign evaluation result to Java variable
How to make a Maven project
Try to create a server-client app
How to pass Oracle Java Silver
Pass environment variables to docker container
Try to make a peepable iterator
How to make a Java array
To not be a static uncle
How to change the value of a variable at a breakpoint in intelliJ
How to pass a proxy when throwing REST over SSL in Java