Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails ActiveRecord::StatementInvalid(PG::SyntaxError:ERROR:SyntaxError:语法错误位于或接近于;_Ruby On Rails_Postgresql_Activerecord - Fatal编程技术网

Ruby on rails ActiveRecord::StatementInvalid(PG::SyntaxError:ERROR:SyntaxError:语法错误位于或接近于;

Ruby on rails ActiveRecord::StatementInvalid(PG::SyntaxError:ERROR:SyntaxError:语法错误位于或接近于;,ruby-on-rails,postgresql,activerecord,Ruby On Rails,Postgresql,Activerecord,我不确定为什么我的查询在本地主机上工作,但在服务器上失败。 当我尝试创建一个路由到QuizzesController#new的测验时,就会发生这种情况 以下是查询: SELECT COUNT(*) FROM "questions" INNER JOIN "question_categories" ON "question_categories"."question_id" = "questions"."id" WHERE "questions"."deleted_at" IS NULL AND

我不确定为什么我的查询在本地主机上工作,但在服务器上失败。 当我尝试创建一个路由到QuizzesController#new的测验时,就会发生这种情况

以下是查询:

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...
quick.rb 在创建之前,我会运行build_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

我相信你使用了错误的引语:

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)
                                                                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
使用
代替``

更新: 是的,我在你的测验模型中是对的,你使用了错误的引语:

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)
                                                               ^^^^^^^^^^^^^^^^^^^^^

你能发布本地和服务器数据库模式吗?去掉表名和列名中的所有双引号。你在开发中也使用Postgres还是SQLite。这是你手工编写的查询吗?否则,添加触发查询的代码以获得更有用的答案。我在开发中使用SQLite,所以我没有手工编写查询我不知道如何更改它。您好,谢谢您的回复!我在开发中使用sqlite,我没有手工编写查询,因此我不确定如何更改它。此外,您正在使用错误中显示的
postgres
(PG::SyntaxError:ERROR:syntax ERROR at或附近出现语法错误。“
PG
postgres
 def build_parts
   category_ids = self.categories.map(&:id)
   question_pool = Question.joins(:question_categories).where('"question_categories"."category_id" IN (?)', category_ids)
                                                               ^^^^^^^^^^^^^^^^^^^^^