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