ruby-on-rails - 如何在 Rails 4 中将 where 条件与 "or"链接起来?

标签 ruby-on-rails ruby-on-rails-4 activerecord

在我的 Rails 4 应用程序中,我需要查找所有计划,其间隔 金额0

这不起作用:

class Plan < ActiveRecord::Base

  def self.by_interval(interval)
    where("interval = ? OR amount = ?", interval, 0)
  end

end

我收到此错误:

Mysql2::Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '= 'month' OR amount = 0)  ORDER BY amount DESC' at line 1: SELECT `plans`.* FROM `plans`  WHERE (interval = 'month' OR amount = 0)  ORDER BY amount DESC

还有什么可能有效?

感谢您的帮助。

最佳答案

mysql中的'interval'是一个保留字(http://dev.mysql.com/doc/refman/5.6/en/reserved-words.html)。

尝试这样:

def self.by_interval(interval)
  where("`interval` = ? OR amount = ?", interval, 0)
end

注意“间隔”周围的反引号(不是引号)

关于ruby-on-rails - 如何在 Rails 4 中将 where 条件与 "or"链接起来?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27734281/

相关文章:

ruby-on-rails - 在 Heroku 日志中找不到错误

mysql - SQL 查询通过关联模型选择用户的 friend

ruby-on-rails - Heroku、Twitter api 上的 Rails 未初始化常量错误,在本地工作正常

ruby-on-rails - 检查关系是否存在 has_one mongoid

ruby-on-rails - rails 4 中的多个数据库连接

ruby - 我怎样才能让 ruby​​-debug-ide 工作?

ruby-on-rails - Ruby on Rails - ActiveRecord::Relation 计数方法错误?

ruby-on-rails - ActiveRecord 或 Postgresql 创建新记录时设置错误的 ID

ruby-on-rails - protected_from_forgery 与 Firefox

ruby-on-rails - 如何使用 Assets 管道在元素 div 中添加 style="background-image?