dimanche 8 mars 2015

Ruby on Rails Pluck attribute from search and search another table with this attribute

Ok, in my Ruby on Rails application I am creating a cinema application. I am trying to implement a search where a user can click on a date, e.g. today or tomorrow, and all the films that have showings for those dates will be returned.


It is basically this: http://ift.tt/1pf4Jwb where a user is able to select a different date.


I will provide my code and then explain the problem I'm having.


views/_search.html.erb:



<%= form_tag my_path do %>
<% date = Date.today %>
<%= hidden_field_tag :search_string, date.strftime("%F") %>

<div class="tab-content">
<%= submit_tag "What's on", class: "btn" %>
</div>

<% date = Date.today + 1 %>
<%= hidden_field_tag :search_string, date.strftime("%F") %>
<%= submit_tag date.strftime("%a %D"), class: "btn" %>

<% date = Date.today + 2 %>
<%= hidden_field_tag :search_string, date.strftime("%F") %>
<%= submit_tag date.strftime("%a %D"), class: "btn" %>

<% date = Date.today + 3 %>
<%= hidden_field_tag :search_string, date.strftime("%F") %>
<%= submit_tag date.strftime("%a %D"), class: "btn" %>

<% date = Date.today + 4 %>
<%= hidden_field_tag :search_string, date.strftime("%F") %>
<%= submit_tag date.strftime("%a %D"), class: "btn" %>

<% date = Date.today + 5 %>
<%= hidden_field_tag :search_string, date.strftime("%F") %>
<%= submit_tag date.strftime("%a %D"), class: "btn" %>

<% date = Date.today + 6 %>
<%= hidden_field_tag :search_string, date.strftime("%F") %>
<%= submit_tag date.strftime("%a %D"), class: "btn" %>
<% end %>


routes.rb:



post 'films/search', to: 'films#search'


films_controller.rb:



def search
@films = Film.date_search(params[:search_string])
else if @films.empty?
flash.now[:alert] = "There are no films showing with that date - Showing all films."
@films = Film.all.order :title
end
render :action => "index"
end


film.rb:



def self.date_search(search_string)
film = Showing.where("show_date = ?", search_string).pluck(:film_id)
self.where("id = ?", film)
end


Associations - film.rb:



has_many :showings
belongs_to :certificate
belongs_to :category


Associations - showing.rb:



belongs_to :film
has_many :bookings
belongs_to :screen


Schema:



create_table "showings", force: :cascade do |t|
t.date "show_date"
t.time "show_time"
t.integer "film_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "screen_id"
end

create_table "films", force: :cascade do |t|
t.string "title"
t.string "synopsis"
t.string "director"
t.string "cast1"
t.string "cast2"
t.string "cast3"
t.date "release_date"
t.string "warnings"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "image_url"
t.string "certificate_id"
t.integer "category_id"
t.integer "hours"
t.integer "minutes"
t.string "video_url"
end


I get the following error:



ActiveRecord::StatementInvalid in FilmsController#search
SQLite3::SQLException: near ",": syntax error: SELECT COUNT(*) FROM "films" WHERE (id = 3,6)


I understand why I get the error but can someone please help me solve it.


Aucun commentaire:

Enregistrer un commentaire