Ruby on Rails Pluck属性来自搜索并搜索具有此属性的另一个表

时间:2015-03-08 15:43:53

标签: ruby-on-rails ruby ruby-on-rails-3 search uniqueidentifier

好的,在我的Ruby on Rails应用程序中,我正在创建一个影院应用程序。我正在尝试实现用户可以点击日期的搜索,例如今天或明天,所有有关这些日期放映的电影都将被退回。

基本上是这样的:http://www.odeon.co.uk/cinemas/covent_garden/81/用户可以选择不同的日期。

我将提供我的代码,然后解释我遇到的问题。

视图/ _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>

    <% (1..6).each do |i| %>
      <% date = Date.today + i %>
      <%= hidden_field_tag :search_string, date.strftime("%F") %>
      <%= submit_tag date.strftime("%a %D"), class: "btn" %>
    <% end %>
<% 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

协会 - film.rb:

has_many :showings
belongs_to :certificate
belongs_to :category

协会----显示.rb:

belongs_to :film
has_many :bookings
belongs_to :screen

架构:

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

我收到以下错误:

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

我理解为什么会收到错误,但有人可以帮我解决。

2 个答案:

答案 0 :(得分:1)

最直接的问题是date_search方法中的以下行:

self.where("id = ?", film)

由于film是一个数组,因此SQL术语中的正确语法应为:

self.where("id IN (?)", film)

要处理这两种情况,即单值和数组,您需要这样:

self.where(id: film)

然而您的date_search类方法实际上应该是在showings上应用内部联接的范围。这是代码:

# in your Film model
scope :date_search, ->(ss){ joins(:showings).where(showings: {show_date: ss}).group("films.id") }

答案 1 :(得分:1)

看起来你是搜索方法的where子句想要一条记录(id),但是它得到了一个数组:

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

应该是

def self.date_search(search_string)
  film = Showing.where("show_date = ?", search_string).pluck(:film_id)
  # film is an ActiveRecord relation (like an array)
  self.where(id: film) # this will then run where(id IN (3, 6)), which are the values in your error in the original post 
end

长和短是您的film =表达式返回的结果超过1,因此where(id = ?)永远不会运行而不会出错。它必须是IN

def self.date_search(search_string)
  film = Showing.where("show_date = ?", search_string).first.pluck(:film_id)
  # if you add .first, then it will only return one record but that may not be what you want. 
  self.where("id = ?", film)
end