ruby-on-rails - 在 where 子句中将字符串转换为数字

标签 ruby-on-rails activerecord

我有以下 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/

相关文章:

ruby-on-rails - 在 Rails 3 中按数组索引选择对象

ruby-on-rails - Rails:after_create中的异常停止保存

ruby-on-rails - 使用 has_many 时弃用警告 :through :uniq in Rails 4

ruby-on-rails - 如何使用 rails 在 redis 中存储复杂模型?

css - Michael Hartl/Ruby on rails、bootstrap、示例应用程序、 header css 无法按预期工作/格式化

ruby-on-rails - 在 Rails 中根据英语词典验证单词?

ruby-on-rails - 在另一个网站中嵌入用 Ruby on Rails 编码的网页?

ruby-on-rails - 如何找出我的系统上安装的 gem 的分支/原始版本?

activerecord - ActiveRecord通过表的位置和顺序

ruby-on-rails - FactoryGirl has_many 与双向存在验证的关联创建额外记录