- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我不确定为什么我的查询在本地主机上有效,但在服务器上却失败了。当我尝试创建一个路由到 QuizzesController#new
的测验时,就会发生这种情况# GET /quizzes/new
def new
@quiz = current_user.quizzes.new
end
这是查询:
SELECT COUNT(*) FROM "questions" INNER JOIN "question_categories" ON "question_categories"."question_id" = "questions"."id" WHERE "questions"."deleted_at" IS NULL AND (`question_categories`.`category_id` IN (87,1))
(1.0ms) ROLLBACK
Completed 500 Internal Server Error in 58ms (ActiveRecord: 13.4ms)
我得到了这样的错误。
ActiveRecord::StatementInvalid (PG::SyntaxError: ERROR: syntax error at or near "." LINE 1: ...s"."deleted_at" IS NULL AND (`question_categories`.`category...
测验.rb在创建之前,我会运行 build_parts,它应该随机抓取问题并将它们放入测验中。 测验类 < ActiveRecord::Base 属于:用户 属于:主题 有很多:测验类别 has_many :categories, 通过: :quiz_categories has_many :quiz_parts
accepts_nested_attributes_for :categories
accepts_nested_attributes_for :quiz_parts
validates :user, :subject, :number_of_questions, presence: true
validates :number_of_questions, numericality: { only_integer: true, greater_than_or_equal_to: 1 }
before_create :build_parts
before_save :set_completed_at, if: -> { completeness == 100.00 }
def completeness
answerable_quiz_parts = 0
quiz_parts.each do |q_part|
answerable_quiz_parts += 1 if q_part.answerable.answers.present?
end
quiz_parts.joins(:choice).count.to_f * 100 / answerable_quiz_parts
end
def score
quiz_parts.joins(:choice).where('choices.correct = ?', true).count { |qp| qp.choice.correct? }
end
private
# select random questions
def build_parts
category_ids = self.categories.map(&:id)
question_pool = Question.joins(:question_categories).where('`question_categories`.`category_id` IN (?)', category_ids)
#self.number_of_questions = [number_of_questions, question_pool.size].min
puts question_pool.size
if number_of_questions > question_pool.size
errors.add(:number_of_questions, 'is too high. Please select a lower question count or increase category selections')
return false
end
number_of_questions.times do |i|
question_pool.inspect
self.quiz_parts << question_pool[i].quiz_parts.new
question_pool[i].question_parts.each do |question_part|
self.quiz_parts << question_part.quiz_parts.new
end
end
end
def set_completed_at
self.completed_at = Time.zone.now
end
end
quizzes_controller.rb
class QuizzesController < ApplicationController
before_action :authenticate_user!
before_action :set_quiz, only: [:show, :edit, :update, :destroy]
# GET /quizzes
# GET /quizzes.json
def index
@quizzes = current_user.quizzes.order(created_at: :desc)
end
# GET /quizzes/1
# GET /quizzes/1.json
def show
end
# GET /quizzes/new
def new
@quiz = current_user.quizzes.new
end
# GET /quizzes/1/edit
def edit
end
# POST /quizzes
# POST /quizzes.json
def create
@quiz = current_user.quizzes.new(quiz_create_params)
respond_to do |format|
if @quiz.save
format.html { redirect_to edit_quiz_path(@quiz), notice: 'Quiz was successfully created.' }
format.json { render :show, status: :created, location: @quiz }
else
format.html { render :new }
format.json { render json: @quiz.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /quizzes/1
# PATCH/PUT /quizzes/1.json
def update
respond_to do |format|
if @quiz.update(quiz_update_params)
format.html { redirect_to @quiz, notice: 'Quiz was successfully updated.' }
format.json { render :show, status: :ok, location: @quiz }
else
format.html { render :edit }
format.json { render json: @quiz.errors, status: :unprocessable_entity }
end
end
end
# DELETE /quizzes/1
# DELETE /quizzes/1.json
def destroy
@quiz.destroy
respond_to do |format|
format.html { redirect_to quizzes_url, notice: 'Quiz was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_quiz
@quiz = current_user.quizzes.find(params[:id])
end
# For quiz setup
def quiz_create_params
params.require(:quiz).permit(:subject_id, :number_of_questions, category_ids: [])
end
# For quiz answering
def quiz_update_params
params.require(:quiz).permit(quiz_parts_attributes: [:id, choice_attributes: [:id, :content, :answer_id, :_destroy]])
end
end
架构.rb:
ActiveRecord::Schema.define(version: 20150726180000) do
create_table "admins", force: :cascade do |t|
t.string "email"
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "sign_in_count", default: 0, null: false
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
t.string "confirmation_token"
t.datetime "confirmed_at"
t.datetime "confirmation_sent_at"
t.datetime "created_at"
t.datetime "updated_at"
end
add_index "admins", ["confirmation_token"], name: "index_admins_on_confirmation_token", unique: true
add_index "admins", ["email"], name: "index_admins_on_email", unique: true
add_index "admins", ["reset_password_token"], name: "index_admins_on_reset_password_token", unique: true
create_table "answers", force: :cascade do |t|
t.integer "number"
t.text "content"
t.boolean "correct", default: false, null: false
t.integer "answerable_id"
t.string "answerable_type"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
add_index "answers", ["answerable_type", "answerable_id"], name: "index_answers_on_answerable_type_and_answerable_id"
create_table "categories", force: :cascade do |t|
t.string "name"
t.integer "subject_id"
t.integer "category_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
add_index "categories", ["category_id"], name: "index_categories_on_category_id"
add_index "categories", ["subject_id"], name: "index_categories_on_subject_id"
create_table "choices", force: :cascade do |t|
t.string "content"
t.integer "quiz_part_id"
t.integer "answer_id"
t.boolean "correct"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
add_index "choices", ["answer_id"], name: "index_choices_on_answer_id"
add_index "choices", ["quiz_part_id"], name: "index_choices_on_quiz_part_id"
create_table "ckeditor_assets", force: :cascade do |t|
t.string "data_file_name", null: false
t.string "data_content_type"
t.integer "data_file_size"
t.integer "assetable_id"
t.string "assetable_type", limit: 30
t.string "type", limit: 30
t.integer "width"
t.integer "height"
t.datetime "created_at"
t.datetime "updated_at"
end
add_index "ckeditor_assets", ["assetable_type", "assetable_id"], name: "idx_ckeditor_assetable"
add_index "ckeditor_assets", ["assetable_type", "type", "assetable_id"], name: "idx_ckeditor_assetable_type"
create_table "levels", force: :cascade do |t|
t.string "name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "question_categories", force: :cascade do |t|
t.integer "question_id"
t.integer "category_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
add_index "question_categories", ["category_id"], name: "index_question_categories_on_category_id"
add_index "question_categories", ["question_id"], name: "index_question_categories_on_question_id"
create_table "question_parts", force: :cascade do |t|
t.text "content"
t.string "type"
t.integer "question_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.datetime "deleted_at"
end
add_index "question_parts", ["deleted_at"], name: "index_question_parts_on_deleted_at"
add_index "question_parts", ["question_id"], name: "index_question_parts_on_question_id"
create_table "questions", force: :cascade do |t|
t.text "content"
t.string "type"
t.integer "level_id"
t.integer "subject_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.datetime "deleted_at"
t.string "source"
end
add_index "questions", ["deleted_at"], name: "index_questions_on_deleted_at"
add_index "questions", ["level_id"], name: "index_questions_on_level_id"
add_index "questions", ["subject_id"], name: "index_questions_on_subject_id"
create_table "quiz_categories", force: :cascade do |t|
t.integer "category_id"
t.integer "quiz_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
add_index "quiz_categories", ["category_id"], name: "index_quiz_categories_on_category_id"
add_index "quiz_categories", ["quiz_id"], name: "index_quiz_categories_on_quiz_id"
create_table "quiz_parts", force: :cascade do |t|
t.integer "quiz_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "answerable_id"
t.string "answerable_type"
end
add_index "quiz_parts", ["answerable_type", "answerable_id"], name: "index_quiz_parts_on_answerable_type_and_answerable_id"
add_index "quiz_parts", ["quiz_id"], name: "index_quiz_parts_on_quiz_id"
create_table "quizzes", force: :cascade do |t|
t.integer "user_id"
t.datetime "completed_at"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "subject_id"
t.integer "number_of_questions"
end
add_index "quizzes", ["subject_id"], name: "index_quizzes_on_subject_id"
add_index "quizzes", ["user_id"], name: "index_quizzes_on_user_id"
create_table "subjects", force: :cascade do |t|
t.string "name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "users", force: :cascade do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "sign_in_count", default: 0, null: false
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
t.datetime "created_at"
t.datetime "updated_at"
end
add_index "users", ["email"], name: "index_users_on_email", unique: true
add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
end
最佳答案
我相信你使用了错误的引号:
SELECT COUNT(*) ....... (`question_categories`.`category_id` IN (87,1))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
使用 ""
而不是 ``
是的,我在你的测验模型中是正确的,你使用了错误的引号:
def build_parts
category_ids = self.categories.map(&:id)
question_pool = Question.joins(:question_categories).where('`question_categories`.`category_id` IN (?)', category_ids)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
将其修复为:
def build_parts
category_ids = self.categories.map(&:id)
question_pool = Question.joins(:question_categories).where('"question_categories"."category_id" IN (?)', category_ids)
^^^^^^^^^^^^^^^^^^^^^
关于ruby-on-rails - ActiveRecord::StatementInvalid (PG::SyntaxError: ERROR: "."处或附近的语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31652896/
创建一个 activerecord 查询并调用 last,它有一个使用 MYSQL 的 order by field 的订单会抛出一个 StatementInvalid 异常。 例如: ruby-1.
我正在尝试使用 Project.find(id) 从 Project 模型中找到一个项目,但它给了我 ActiveRecord::StatementInvalid错误 完整跟踪- PG::Error
我正在尝试运行刚刚有的users_test.rb文件 test "the truth" do assert true end 我确实有一个喜欢表,但仍然收到此错误。为什么这样? You
我几天前开始学习 rails3,从今天开始,每次我为我的模型运行单元测试时,我都会收到此错误消息: ActiveRecord::StatementInvalid: SQLite3::SQLExcept
我正在学习 Ruby on Rails 教程,我已经完成了第 7 章并且运行良好,但是遇到了 25 个失败/错误,如下所示: 用户 失败/错误:@user = User.new(名称:“示例用户”,电
我是 ruby on Rails 的学习者,我想加入两个模型(表)。User.rb(用户表) class User :candidate_id) end 结束 运行我的代码时出现错误
我们刚刚将 Rails 应用程序从 3.0.13 更新到 3.2.6,并且在使用 mysql2 的 Active Record 查询接口(interface)的 .where 方法时遇到了一个小问题。
有谁知道如何解决 rails 中的 ActiveRecord::StatementInvalid 错误?控制台显示“PG::InvalidTextRepresentation: ERROR: inva
这是完整的错误信息 ActiveRecord::StatementInvalid: PG::DataCorrupted: 错误:无法读取文件“base/50711/50722”中的 block 0:只
我是一名开发 Sinatra/Ruby/ActiveRecord 应用程序的新开发人员。我已经在 Category 和 Recipe 之间建立了 1:many (postgres) 关系。当我尝试列出
我有一个复选框字段 (car_options),用于存储多个选项的值数组。 我需要查询包含另一个数组中任何值的所有 Car 记录。 ["a","b","c","d","e","f"] 包含以下任何一项
我正在尝试创建一个 ActiveRecord 对象。但是我在创建它时遇到了这个错误。 (0.1ms) ROLLBACK ActiveRecord::StatementInvalid: PG::InF
在我的rails项目中,我使用sidekiq处理耗时任务,但是在sidekiq中记录了一个错误: ActiveRecord::StatementInvalid: Mysql2::Error: Lock
我之前将我的模型错误地命名为“公司”。我没有重命名它,而是重命名了所有引用,删除了数据库并通过重新运行迁移重新创建了所有内容。我能够从 SQLite 资源管理器中看到重新创建为“公司”的表,但是当我通
将 rails 应用程序从 rails3.2 升级到 rails 4 时出现问题 ActiveRecord::StatementInvalid in SessionsController#new My
我在 Ruby on Rails 上使用 ActiveRecord 和 oracle 适配器。尝试删除行时出现 StatementInvalid Exception。 这是我的 table 的样子:r
cis.statbib.org: script/console Loading development environment (Rails 2.2.2) Article.founc>> Articl
我使用 rails_admin 作为管理面板。只需更改图像模型中的关联 由此 class Image < ApplicationRecord belongs_to :user belong
在RSpec中运行测试时,出现以下错误: Failures: 1) User micropost associations should have the right micropost in t
在我的 Rails 应用程序中,我有一个脚本可以更新数据库中的一些记录。当我发送一个 SIGTERM 来终止脚本时,它偶尔会在 ActiveRecord 执行查询时收到该信号。这会导致引发 Activ
我是一名优秀的程序员,十分优秀!