Question

I'm using ransack and I have a Venue model which has a many to many relationship with stages (draft, edit, done, etc).

Here is my form

= search_form_for @q, url: search_admin_venues_path, html: {method: :post} do |f|
  = f.label :name_cont
  = f.text_field :name_cont
  = f.select :stages_id_eq, Stage.all.collect{|c| [c.name, c.id]}, {include_blank: true}

Is there a way to search for venues which are not part of the selected stage? ie: reject them somehow?

Here is the ransack search I use

  def search
    @q = Venue.search(params[:q])
    @venues = @q.result(distinct: true)    
    render :index
  end

Here is what I used to do:

stage_obj = Stage.find_by_name(params[:stage])
@venues = Venue.all.includes(:stages).reject{|v| v.stages.include?(stage_obj)}
Was it helpful?

Solution

Using not_eq predicate should work, as Viren noticed in comment.

= f.select :stages_id_not_eq, Stage.all.collect{|c| [c.name, c.id]}, {include_blank: true}

You also may include or exclude multiple values using multiple select input and in/not_in predicates.

OTHER TIPS

Have you seen the railscast video on ransack?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top