Home>
ransack
I would like to create a search form using and realize a multi-word search separated by spaces.
You can do "Search multiple words from a single column" or "Search a single word from multiple columns", but you cannot "Search multiple words from multiple columns". I tried to refer to the answer of the person who was asked in the past, but the SQL before separating by space separation is reissued and overwritten.if statement
I tried to make a branch with, but it doesn't work. Can anyone please teach me?
before_action: set_search
def set_search
@categories = Category.where (ancestry: nil)
if params [: q] .present?
if params [: q] [: title_or_body_or_material_cont_any]! = nil
texts = params [: q] [: title_or_body_or_material_cont_any] .split (/ [\ p {blank} \ s] + /)
if texts.size>= 2 # If i do not branch whether it is multiple words, an error will occur in the case of a single word
text_queries = texts.map do | text |
Recipe.ransack (title_or_body_or_material_cont_any: text) .result
end
text_queries = text_queries.inject {| scope, query | scope&query}
@search = text_queries.inject {| scope, query | scope.merge (query)} # (title or body or material) and (title or body or material)
@search_recipes = @search # If i add ".page (params [: page]). Per (8)", an error will occur.
@text_queries = text_queries # For checking the contents
end
end
@search = Recipe.ransack (params [: q]) # Probably SQL overwrite occurs here?
@search_recipes = @ search.result.page (params [: page]). per (8)
else else
params [: q] = {sorts:'created_at desc'}
@search = Recipe.ransack ()
@search_recipes = @ search.result.page (params [: page]). per (8)
end
end
It's a messy source because of trial and error, but I would appreciate it if you could also teach me how to write it concisely.
-
Answer # 1
Related articles
- ruby - i want to search for form information in rails and save the result
- ruby on rails - about image distribution on heroku and s3
- ruby on rails - about the argument of rubyonrails params
- ruby on rails - about matters that cannot be migrated
- ruby on rails - about the description in applicationjs
- ruby - about routing using rails collection
- ruby on rails - about saving comments
- ruby on rails - about nomethoderror in skilscontroller # create
- ruby on rails - about rails composite indexes
- ruby on rails - [rails] about the problem that screen transition is not possible (error does not occur)
- ruby - about rails controller new action
- ruby - [rails] about the error that occurs when deleting likes (routing error)
- ruby - [rails] about nameerror
- ruby - about rails routing
- ruby on rails - i want to display search results
- ruby on rails - about primary key/natural key/surrogate key in db design
- ruby on rails - about the matter that does not transition to the detail page
- ruby - [rails] implementation of search function
- ruby - about embedding rails videos
Trends
- python - you may need to restart the kernel to use updated packages error
- php - coincheck api authentication doesn't work
- php - i would like to introduce the coincheck api so that i can make payments with bitcoin on my ec site
- [php] i want to get account information using coincheck api
- the emulator process for avd pixel_2_api_29 was killed occurred when the android studio emulator was started, so i would like to
- javascript - how to check if an element exists in puppeteer
- dart - flutter: the instance member'stars' can't be accessed in an initializer error
- sh - 'apt-get' is not recognized as an internal or external command, operable program or batch file
- i want to call a child component method from a parent in vuejs
- python 3x - typeerror: 'method' object is not subscriptable
When this is confusing, I combine normal search with Ransack.
First
@q = Recipe.ransack (params [: q])
However, this is only used to redisplay the search conditions on the result display screen, not for searching.title_or_body_or_material_cont_any = params [: q] .delete (: title_or_body_or_material_cont_any)
And create a search condition using title_or_body_or_material_cont_any
relation = Recipe. [Search condition using title_or_body_or_material_cont_any] @search_recipes = relation.result.page (params [: page]). per (8)
Get in
Postscript
@categories = Category.where (ancestry: nil) if params [: q] .present? else else
Change between.