How to create search conditions involving multiple models

This time, the product name (name) and product description (description) saved in the items table A keyword from the tag name (tag _name) values stored in the tags table I want to create an implementation of a searchable function. (The items table and the tags table are associated using an intermediate table in a many-to-many relationship.)

Create your own method in 1 item model, define the search conditions from each table and assign them to variables (@item, @sescription and @tag) 2 Create an empty array and assign it to a variable (@items) 3 Get it as one element by applying each method and add it to the array. (As for tags, one product may have multiple tags, so double each is applied.) 4 Use the uniq method to remove the overlapping elements. (Example: When you search for "apple", if the keyword "apple" is included in both the product name @item and the product description @description, the same product will be included in the array @items. Because) 5 Finally, explicitly describe the return value and send the data to params (@items)

item.rb


def self.item_search(search)
    if search != ""
      @item = Item.where('name LIKE(?)', "%#{search}%")
      @description = Item.where('description LIKE(?)', "%#{search}%")
      @tag = Tag.where('tag_name LIKE(?)', "%#{search}%")
      @items = []
       @item.each do |i| 
         @items << i
       end
       @description.each do |d|
        @items << d
       end
      @tag.each do |t|
        t.items.each do |ta|
          @items << ta
        end
      end
      @items = @items.uniq
      return @items

    else
      return nil
    end
  end


end

6 Call the "item_search method" created in the model earlier with the controller.

items-controller.rb


 def item_search
    @items = Item.item_search(params[:keyword])
  end

7 Extract the elements contained in the array @items using the each method.

item_search.html


<% @items.each do |i| %>
  <% =i.name%>
[Omitted]
<% end %>




Recommended Posts

How to create search conditions involving multiple models
How to search multiple columns with gem ransack
How to create multiple pull-down menus with ActiveHash
How to create docker-compose
[Rails] How to search by multiple values ​​with LIKE
(Ruby on Rails6) How to create models and tables
How to create an application
How to create a method
[Swift] How to replace multiple strings
How to implement search functionality in Rails
How to implement search function with rails (multiple columns are also supported)
How to create a Maven repository for 2020
How to update related models with accepts_nested_attributes_for
[Swift5] How to create a splash screen
[rails] How to create a partial template
How to switch between multiple Java versions
How to execute multiple commands in docker-compose.yml
[Spring sample code included] How to create a form and how to get multiple records
How to create a database for H2 Database anywhere
[Rails] How to create a graph using lazy_high_charts
How to save to multiple tables with one input
How to create pagination for a "kaminari" array
[Rails] How to implement unit tests for models
How to create a class that inherits class information
How to create a theme in Liferay 7 / DXP
[Rails] How to upload multiple images using Carrierwave
[1st] How to create a Spring-MVC framework project
How to install multiple JDKs on Ubuntu 18.04 LTS
How to easily create a pull-down in Rails
Rails6.0 ~ How to create an eco-friendly development environment
[Rails] How to create a Twitter share button
How to create hierarchical category data using ancestry
How to create member variables with JPA Model
How to define multiple orm.xml in Spring4, JPA2.1
Notes on how to create Burp Suite extensions
How to create an oleore certificate (SSL certificate, self-signed certificate)
[Java] How to make multiple for loops single
[Rails] How to search across columns of related models (parent or child) in ransack
How to change the action with multiple submit buttons
How to create a Java environment in just 3 seconds
How to create a JDBC URL (Oracle Database, Thin)
How to create a Spring Boot project in IntelliJ
How to create a data URI (base64) in Java
[For those who create portfolios] How to use font-awesome-rails
How to create and execute method, Proc, Method class objects
Rails learning How to implement search function using ActiveModel
[Apple Subscription Offer] How to create a promotional offer signature
[Ruby] How to extract a specific value from an array under multiple conditions [select / each]