Skip to main content

Forum Series Part 6: Search with Ransack Discussion

General • Asked by Chris Oliver

Thanks for the great video Chris!

Does ransack support autocomplete while typing in a search query? I checked out the documentation on github, but it doesn't mention anything about it.

Also, what do you think about Searchkick (https://github.com/ankane/s... Would love to see a video on this too!

Thanks again!

You can do autocomplete by having it query the URL for JSON response each time someone types a character. My suggestion is to try hooking it up with jQuery UI's autocomplete. http://jqueryui.com/autocom...

Awesome! Thanks, Chris.


Just implemented this in conjunction with Ryan's episode 370 http://railscasts.com/episo.... Have some really cool advanced search features now working on my customer index page.

Now just need to figure out how to rspec this stuff (I find BDD doesn't work in the learning phase).


@excid3:disqus in the controller why isn't it ForumThread.search(params[:subject_cont]) ?

You need to pass in params[:q] so it passes in both the name of the field and the search you want to do on it. Since it supports multiple fields in search, you pass them all in instead of just one.

@excid3:disqus but why isn't :subject_cont being passed through the params hash? Is this central ransack, grabbing the object? Normally if i had an input field like f.text_area :body , I would get it by params[:body] in the controller, no?

You're right that you should get body in the controller in params. In the controller you should get params[:q][:subject_cont] with Ransack.

one more thing: because ransack returns the entire list when q is empty, we don't need to have a conditional like ForumThread.all if params[:q] is empty or nil. Does ransack scope the list in a certain order or can we set up a scope?

You can apply scopes to ransack search results to also include it in the query. @q.result.my_scope


@excid3:disqus Do you know how to include a has_many through relationship in the search? Specifically, I'd like to search for tags associated with a recipe. The setup is similar to acts_as_taggable but I rolled my own so to speak. I tried :name_or_category_or_tags_cont but got an error.

It looks like on the Ransack docs, they do this:

class Artist
has_many :memberships
has_many :musicians, through: :memberships
end

artists = Artist.ransack(musicians_email_cont: 'bar')

added 'or_tags_name_cont' to the search field tag and it seems to work; thanks for the tip, Chris.


Hey Chris! Nice one! I'm already using ransack gem based on this tutorial. On the top of this for some models in my app I would like to take this one step further by using some AJAX/jQuery autocomplete search/sorting for my table. I found a bunch of things, but every given solution I found seemed to be outdated. As of Nov 2015, could you tell me what gems/jQuery plug-ins/etc I should use to implement both the ajaxified searching and sorting feature that will be good enough in production? Thanks!


Great video! I want to download the search results into an Excel file and am able to kind of do this. I'm having 2 problems.

First, when I use the @projects variable below, it works but only allows me to download 40 results as per the will_paginate gem.

@projects = @q.result.includes(:vendor, :service_category).paginate(:page => params[:page], :per_page => 40)

Second, I have a standard download (with many tabs) that is not dependent on the search results. I want there to be two exports on the projects index page- one which is the standard spreadsheet and the other that only includes the search results. Can I do this in the index action?

This is what I have in my respond_to block

format.xls { response.headers['Content-Disposition'] = "attachment; filename=\"projects_list_created_on_#{formatted_date_today}.xls\""
and there is a corresponding index.xls.erb file in the views.

Thank you in advance for any advice!


How would you add ransack if your post controller looks like this?

def index
if params[:category].blank?
@posts=Post.all.order("created_at desc").paginate(:page => params[:page], per_page: 15)
else
@category_id = Category.find_by(name: params[:category]).id
@posts = Post.where(category_id: @category_id).order("created_at DESC").paginate(:page => params[:page], per_page: 2)
end


Hi Chris, I added Ransack to search for users and the search field always is pre-filled with the logged-in user's email and has to be deleted manually. Before a search the field is pre-filled again. Any hint on what's going on?


Do I need to have eelastic search installed for this setup or searchkick?

I basically want to get this to work : https://gorails.com/episodes/global-autocomplete-search?autoplay=1

By following this tutorial will I have the setup needed to do the above link as well?


Login or Create An Account to join the conversation.

Subscribe to the newsletter

Join 24,647+ developers who get early access to new screencasts, articles, guides, updates, and more.

    By clicking this button, you agree to the GoRails Terms of Service and Privacy Policy.

    More of a social being? We're also on Twitter and YouTube.