Skip to main content

Join GoRails to continue learning

Subscribe to GoRails to get access to this episode and all other pro episodes, and new awesome content every month.

Subscribe Now
Only $19/month

Login to your account

26 Forum Series Part 6: Search with Ransack

Episode 30 · October 30, 2014

Search with Ransack through associations

Gems Search


Transcripts

Subscribe or login to view the transcript for this episode.

Discussion


Gravatar

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!

Gravatar

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...

Gravatar

Awesome! Thanks, Chris.


Gravatar

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).


Gravatar
Kohl Kohlbrenner on

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

Gravatar

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.

Gravatar
Kohl Kohlbrenner on

@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?

Gravatar

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.

Gravatar
Kohl Kohlbrenner on

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?

Gravatar

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


Gravatar
Kohl Kohlbrenner on

@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.

Gravatar

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')

Gravatar
Kohl Kohlbrenner on

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


Gravatar
Sz M (2,710 XP) on

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!


Gravatar

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!


Gravatar
Brian Gilbank on

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


Login or create an account to join the conversation.