我有以下 where 子句:
OnlineCourseRegistration.where(course_class_id: 6, status: "Completed")
=> #<OnlineCourseRegistration id: 142, cart_id: 150, user_id: 7069, course_class_id: 681, created_at: "2017-07-15 22:06:06", updated_at: "2017-07-20 23:59:01", exam_attempts: 1, exam_completed_at: "2017-07-20 23:57:32", evaluation_completed_at: "2017-07-20 23:59:01", status: "Completed", score: "87", add_extension: false, retest_cart_id: nil, retest_purchased_at: nil>
我还想补充一点,分数将大于 70,所以类似......
OnlineCourseRegistration.where(course_class_id: 681, status: "Completed", :score.to_i > 70).last
...但是当然,这是行不通的。有没有办法在 where 子句中做到这一点?
最佳答案
ActiveRecord 接受 string
作为查询构造函数
OnlineCourseRegistration.where("course_class_id = ? AND status = ? AND score > ?", 681, "Completed", 70).last
关于ruby-on-rails - 在 where 子句中将字符串转换为数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46776505/