last day (15 days later) » 

9:52 AM
0
A: Creating advanced search with select options including acts_as_taggable tags search

Mshkait seems that SEARCHABLE_FIELDS being just an array is not enough you are searching Contact only while other fields are in other tables for example in your old search when you search for supplier if looks for either supplier_name or accounts.name (not really sure here) but inside the supplier tab...

 
So would it be possible to reuse the quick_search_fields? In regards to that join though where would that go, based on the code I have above would it be possible for you to flesh out the example more. Trying to learn how this would work as this is a legacy app I'm working on so I'm trying to modernise how this search works.
 
can you please then post the schema for the supplier, customer and if there is a accounts table then that too
 
I've added the accounts table in the same code block for the contacts schema, as for suppliers and customers there's no schemas for that
 
@debugabug I added an example but i do not understand how tags work so i left it out
 
There doe using acts_as_taggable, but I'll give the following a try
 
9:52 AM
added the tagged_with condition
 
Do I need to change anything controller wise? Because the search just returns everything
 
Hey moved into a discussion as per stack overflow guides
i updated the answer, can you try with_indifferent_access on the QUICK_SEARCH_FIELDS
 
Hi
 
10:15 AM
let me know if it works, you might want to remove the guard method return all unless field && query to see the failure
 
11:05 AM
I'll give that a try
 
11:43 AM
OK that worked, if I wanted to replace the customers to content_type if there a supplier or customer, like this

where("contact_type = 'customer' and name like ?", "%#{search}%")
That sql query works but how do I move that into the QUICK_SEARCH_FIELDS hash?
 
you mean the contact_type and the name?
explain more please, do you want the search bar to show a drop-down with name (contacts.name) and a contact_type?
is the contact_type always gonna be customer?
 
12:00 PM
So the customer and supplier should search all records where the contact type is either a customer or supplier
So the customer option should search all records where all the contact_type is customer and the name that is entered into the search box
 
12:20 PM
anh now i understand
```
QUICK_SEARCH_FIELDS = {
name: {
column_names: 'contacts.name'
},
customers: {
joins_table: :customer,
column_names: 'accounts.name',
filters: {contact_type: "customer"}
},
suppliers: {
joins_table: :supplier,
column_names: 'accounts.name',
filters: {contact_type: "supplier"}
},
tags: {
tagged_with: true
}
}.with_indifferent_access


def self.search(field, query)
field = QUICK_SEARCH_FIELDS[field]
return all unless field && query

relation = all
relation = relation.joins(field[:joins_table]) if field[:joins_table]
ruby is able to query with a hash so i added them into a filter fields and then will be added to the relation if field[:filters] is present
 
I don't need to go into the accounts table I'm purely just trying to search the contacts table
Ah no worries
Can I ask you another question?
 
sure?
 
OK so we have a basic search now working (thank you) but we also have an advanced search
I assume I can do roughly the same thing right? However we have dropdowns, text, calendar fields etc
 
12:35 PM
this will be tricky for the advanced search, you will be able to get it running yes but you will need few conditions and what not -> are you able to find a gem that can make this?
if not I would advice extracting this into multi-class query objects dev.to/renatamarques97/… where each class takes care of getting a relation then adding a new relation to it then returning it
I'm glad your basic search is working, if you could be so kind enough to upvote my answer and mark it as correct I would be grateful πŸ™πŸ½
 
The way it seems to work currently is a massive loop if statement like so
https://i.gyazo.com/3c4086b107e61c1be916349ef7829a29.png

and each option is done like so

{
col_name: 'prospect_strength',
title: 'prospect strength',
exact_match: true,
integer_col: true,
type: 'select',
options: [['Any', '']] + Contact::PROSPECT_STRENGTHS
},
{
col_name: 'customer_account_id',
title: 'customer',
exact_match: true,
type: 'search_select',
table_object: Customer,
options_data: {
type: 'global', object_name: 'CustomerSelectData'
I would prefer not to use a gem if possible I was thinking of creating each field separately in the form and just doing it that way, but it seems there is table objects and other stuff so I just want to simply everything if possible
 
ouff that looks complicated, yeah you should simplify things, and use easier naming in the fields options you sent that you could directly link into your SQL so you can easily understand it when re-reading your code
ruby is able to execute sql directly so you can create a class with all the options and just write plain sql
161
Q: How do you manually execute SQL commands in Ruby On Rails using NuoDB

Patrick AngodungI'm trying to manually execute SQL commands so I can access procedures in NuoDB. I'm using Ruby on Rails and I'm using the following command: ActiveRecord::Base.connection.execute("SQL query") The "SQL query" could be any SQL command. Like for example I have a table called "Feedback" and whe...

 
Yeah it's a legacy app 2.2.6 so I'm trying to modernize it before a ruby upgrade, so trying to keep everything as simple as possible.

This code wasn't written by me I'm just dealing with the mess haha
 
haha been there, not a pleasant job, good luck mate
 
Thank you, if I run into any issues I'll post another question. I've upvoted your answer this is my first time doing a search like this so trying to learn as I go
 
12:50 PM
yeah I'll keep an eye if you want for your questions, have a look at query objects in rails, they might make your life easier
bbye πŸ‘‹πŸΌπŸ‘‹πŸΌ
 
If you wouldn't mind, cheers
 

last day (15 days later) »