我必须在Rails上获取ruby中的列的值,我使用find_by_sql在mysql-cmd中获取值,该值正确返回,但在视图页面中它返回一个随机值

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 cmd中

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


提前致谢

最佳答案

因为find_by_sql将返回一个数组

您如何尝试从结果访问值?

你有这样尝试过吗

@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/

10-13 07:20