Finding Rails Rails with check_box for a related model

I got lost when I used ransack, the Rails Serching Stone.

What I want to do is work with checkboxes for the matched model.

Here is my code.

shows_controller.rb

class ShowsController < ApplicationController
def index
    @q = Show.search(params[:q])
    @shows = @q.result(:distinct => true)
    @shows = @shows.joins(:tickets)

    respond_to do |format|
      format.html # index.html.erb                                                                                                       
      format.json { render json: @shows }
    end
end

index.html.erb

<%= search_form_for @q do |f| %>
  <%= f.label "Show Title: " %>
  <%= f.search_field :title_cont %>
  <%= f.label "Buy at:" %>
  <%= check_box_tag 'q[shows_tickets_store_cont[]]', 'venue' %>At Venue
  <%= check_box_tag 'q[shows_tickets_store_cont[]]', 'ticketmaster' %>Ticketmaster
  <%= submit_tag "Find Now" %>
<% end %>

<% @shows.each do |show| %>
  <%= show.title %> | 
  <% show.tickets.each do |ticket| %>
    <%= ticket.store %><br />
  <% end %>
<% end %>

show.rb

class Show < ActiveRecord::Base
  has_many :tickets
end

ticket.rb

class Ticket < ActiveRecord::Base
  belongs_to :show
end

When I wrote something in the search field, checked "check_box" and clicked the "Find Now" button, the log showed as shown below:

Parameters: {"utf8"=>"✓", "q"=>{"title_cont"=>"something", "shows_tickets_store_cont"=>"venue"}, "commit"=>"Find Now"}
Show Load (1.1ms)  SELECT DISTINCT `shows`.* FROM `shows` INNER JOIN `tickets` ON `tickets`.`show_id` = `shows`.`id` WHERE (`shows`.`title` LIKE '%something%') LIMIT 25 OFFSET 0

I have no idea why SQL has no offer WHEREfor Ticket.store, even though the ticket controller got "shows_tickets_title_cont" => "place".

Please suggest a solution for this.

Thanks at Advance.

+3
source share
1 answer

, : shows_tickets_store_con, shows_tickets_store_cont. , _cont.

Ransack:

https://github.com/ernie/ransack

cont (contains) and start (starts with) are just two of the available search predicates. See Constants for a full list.

# 1

.

, . checkboxes, - - predicate. - multiple values ().

SQL:

"shows_tickets_store" IN ('venue','something')

:

:

+4

All Articles