我有以下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子句中执行此操作?
答案 0 :(得分:2)
ActiveRecord,其中string
作为查询构造函数
OnlineCourseRegistration.where("course_class_id = ? AND status = ? AND score > ?", 681, "Completed", 70).last
答案 1 :(得分:0)
如果您不想使用原始SQL,可以使用Arel方法,例如:
OnlineCourseRegistration.where(course_class_id: 681, status: "Completed").where(OnlineCourseRegistration.arel_table[:score].gt(70)).last
答案 2 :(得分:0)
对于后人,我想添加这些cast solutions作为答案:
{{1}}