gpt4 book ai didi

mysql - ruby on rails 中的 find_by_sql 报错

转载 作者:太空宇宙 更新时间:2023-11-03 10:57:05 25 4
gpt4 key购买 nike

我必须在 ruby​​ on rails 中获取列的值我使用 find_by_sql 在 mysql-cmd 中获取值该值正确返回但在 View 页面中它返回一个随机值

passed_question = params[:passed_question]
@answer.questions_id = passed_question
next_question = params[:next_question]
@question = Question.find_by_id(passed_question)
@module = Question.find_by_sql ["SELECT student_additional_field_id FROM questions WHERE id=#{passed_question}"]

它的127450632值但是在 mysql 命令中

mysql> select student_additional_field_id from questions
-> where id=3;
+-----------------------------+
| student_additional_field_id |
+-----------------------------+
| 1 |
+-----------------------------+

提前致谢

最佳答案

因为find_by_sql会返回一个数组

您如何尝试从 result 访问值?

你试过这样吗

@module = Question.find_by_sql ["SELECT student_additional_field_id FROM questions WHERE id=#{passed_question}"]

student_additional_field_id = @module[0].student_additional_field_id

关于mysql - ruby on rails 中的 find_by_sql 报错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19508896/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com