【问题标题】:Application_controller define method to only show future datesApplication_controller 定义仅显示未来日期的方法
【发布时间】:2015-02-20 09:16:40
【问题描述】:

在我的 Ruby on Rails 应用程序中,我有一个影院系统,我试图只显示未来或今天(所以不是过去)的电影的放映时间。

我正在尝试在下拉菜单中的 _form.html.erb 中执行此操作:

<%= f.grouped_collection_select :showing_id, live_films.order(:title), :showings, :title, :id,  :showing_times %>

其中live_films是application_controller.rb中的方法:

class ApplicationController < ActionController::Base
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
protect_from_forgery with: :exception

helper_method :active_menu, :live_films  

def live_films 
    Film.includes(:showings).where('showings.show_date > ?', Date.current.beginning_of_day)
end
end

但我收到此错误:

ActiveRecord::StatementInvalid in Bookings#new
SQLite3::SQLException: no such column: showings.show_date: SELECT "films".* FROM "films" WHERE (showings.show_date > '2015-02-20 00:00:00.000000')  ORDER BY "films"."title" ASC

我的数据库/模式:

ActiveRecord::Schema.define(version: 20150219091141) do

create_table "bookings", force: :cascade do |t|
    t.integer  "user_id"
    t.integer  "showing_id"
    t.integer  "seat_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
end

create_table "categories", force: :cascade do |t|
    t.string   "genre"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
end

create_table "certificates", force: :cascade do |t|
    t.string   "age_rating"
    t.datetime "created_at",          null: false
    t.datetime "updated_at",          null: false
    t.string   "certificate_img_url"
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

create_table "screens", force: :cascade do |t|
    t.string   "name"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
end

create_table "seats", force: :cascade do |t|
    t.string   "row_letter"
    t.integer  "row_number"
    t.integer  "screen_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
end

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

add_index "showings", ["film_id"], name: "index_showings_on_film_id"

create_table "users", force: :cascade do |t|
    t.string   "password_digest"
    t.string   "role"
    t.datetime "created_at",      null: false
    t.datetime "updated_at",      null: false
    t.string   "first_name"
    t.string   "last_name"
    t.string   "house_no"
    t.string   "street"
    t.string   "town"
    t.string   "postcode"
    t.string   "email"
end

列名show_date 是正确的,表名showings 也是正确的,但由于某种原因它不起作用。

有人可以帮忙吗?

【问题讨论】:

  • 显示您的数据库架构(db/schema.rb 文件)。
  • 这可能是因为您尝试在日期类型列上执行日期时间列的操作。尝试仅使用日期(不包括时间)进行范围检查
  • 我该怎么做?
  • 尝试类似Date.current.to_s(:db) 而不是Date.current.beginning_of_day

标签: ruby-on-rails ruby date drop-down-menu where


【解决方案1】:

解决办法如下:

Film.joins(:showings).where('showings.show_date > ?', Date.current.beginning_of_day).preload(:showings)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-13
    • 1970-01-01
    • 2015-10-07
    • 2021-11-25
    相关资源
    最近更新 更多