#111 Advanced Search Form (revised)
It is often best to use a GET request when submitting a search form, however if it is an advanced search form with a lot of fields then this may not be ideal. Here I show how to create a search resource to handle this.
- Download:
- source code
- mp4
- m4v
- webm
- ogv
Thank you Ryan for this advanced search screencast.
Is there any suggestion for us about fulltext search engines? In Ryan`s screencasts, before,
he show us Sphinx and Sunspot, what about Solr?
Regards.
Maybe you could also take a look at Picky.
Murat: Sunspot is a ruby wrapper for Solr. Another search engine to check out is ElasticSearch (and the Tire gem for it). It has some nice features like distributed indices for high availability and it's a lot easier than to set up than solr.
Thank you Daniel.
I will just look at ElasticSearch with Tire gem. I assume that i would integrate ElasticSearch in my project.
I had used ElasticSearch with mebla gem, it was working so good and easy to configure on production. But I will try solr this time, because it's mongoid configuration is so simple.
Murat hocam, sen nerede kullanacaksın fulltext aramayı ?
farukca: I just try to develop a file flowing. We have some papers comming from other companies. These files includes some unordered information. So somebody make ocr or writedown this unordered fields. And searching on it.
Just a small comment on the pruning of old searches. I think it's generally bad behaviour to remove old searches. People might link to it, expect it to remain, and boom, suddenly it's gone.
It's probably better to check if a specific search already exists before creation and redirect to it if need be.
What's wrong with not having a database backend at all, configuring the attribute accessors manually and have the create action of the controller show the search result (after possible validations)? I guess re-using the search has its niceities, but how often will it actually get used in an average app?
There's nothing wrong with that approach either. I think it depends on the specific use case.
I have both strategies in one of my apps. Visitor searches on the public site are not stored, but admin searches in the marketing module are because they provide the criteria for targeting a segment of a marketing list which is then used by a backend job to send emails. Since the list changes as subscribers come and go, having the list based on search criteria is essential.
So this type of advanced search definitely has it's use case. Thanks for the revision Ryan.
I agree with @Dom. Nothing wrong, just different use cases.
For the non-db backed, basic approach I'd make my route a singular resource
resource :search
The controller could just about stay the same, except for the
show
action:And then the model could be something like:
By "non-db backed" I mean you don't have to store every search, but you'd still need some type of storage. It's possible not to use storage by showing the search from the
create
action, but I think it's good practice to only display pages from a GET request.While it's fresh in my brain, here's an example for using the session.
Controller:
Model:
There's probably ways to make this cleaner, but I think the idea is there.
Would love something like this where mongoid is used :)
For some season I am getting:
I'm receiving the same error...
Am also receiving the same error
explicitly rendering the collection partial,
instead of the shortcut
render @search.products
sorts the problem
How would you add search for category name with a text_field instead of id ?
products = Product.include(:category).order......
products = products.where("categories.name like ?", "%bla bla bla%")
Something like this? I know i can make category name an auto complete text search Railscasts #102 Auto-Complete Association (revised) like and just pass the category id.. Just looking for alternative ways..
I have tested this advanced search method for my application and it's working fine.
My question is how can i implement field calculation in that model?
Example for products models showing here, if i need to have "totals of products selected", "total product's cost" and "average cost", where can i put these calculation methods?
I have been going through this railscast but can't seem to get the search results to actually display. When I click on the search button, the log shows that it is doing the search, but the only thing that appears on the view is the heading for the Show page (Search Results). The partial for searches (_product.htlm.erb) does not appear to even be accessed.
Any ideas?
Perhaps it's a dumb question but imagine that you have 400.000 products
in this chain of wheres conditions (with pagination)
The search execute the first, and then filter with the other where condition so you have a 400.000 products search that kill performances, or am I totally (I wish) wrong?
later versions of rails do not perform the search until you actually use the results. So every one of the calls in the code just builds a set of conditions.
it will not hit the database until you use "all" or "each" on "products"
Hi! I started with the code from this screencast, switched it to ModestModel to remove the persistence, and then moved over to Mongoid. Just wanted to say thanks!
Thank you Ryan for this.
Awesome episode! I was wondering though, how might you handle AND statements for searching on a has_many association? The OR (contains any) is simple enough, just an IN statement. I'm curious how this might handle the other situation.
How do you filter a filter by another filter?
Meaning, if I click a category like "Brand", How do I have another filter Model with models from only that brand?
How would I build tests for the "find_products" method?
You would provide different data set to test different search possibilities. For instance, provide keywords and check if it returns the expected products.
Can Anyone help.
I am having an issue to use this in rails 4 app.
I think my hands are growing from an incorrect place as I don't get how you guys doing it. I have nothing but mistakes there.((