【问题标题】:How to write scope where one attribute is less than other?如何编写一个属性小于另一个属性的范围?
【发布时间】:2013-10-27 13:31:05
【问题描述】:

我试过这个

scope :opened, lambda {       
  where("entries_count <= limit")
}

但我得到一个错误:

Mysql2::Error: 你的 SQL 语法有错误;检查手册 对应于您的 MySQL 服务器版本以获得正确的语法 在第 1 行使用接近 'limit)':SELECT shifts.* FROM shifts WHERE (entry_count

【问题讨论】:

  • where("entries_count &lt;= #{self.table_name}.limit") 可能工作
  • Backtick-quoting limit 将是另一个(MySQL 特定的)选项。

标签: mysql ruby-on-rails ruby scope


【解决方案1】:

entry_count 和 limit 活动记录属性是否都映射到列?

scope :opened, -> { where(:entries_count <= :limit }

【讨论】:

    【解决方案2】:

    您可以通过以下方式获得:

    scope :opened, lambda { |limit| where("entries_count <= ?",  limit) }
    

    或者这种方式是更新的 lambda 语法:

    scope :opened, -> limit { where("entries_count <= ?",  limit) }
    

    【讨论】:

    • limit 是取自表中列的值,而不是范围的参数。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-02-07
    • 2020-03-02
    • 2017-04-12
    • 1970-01-01
    • 1970-01-01
    • 2016-07-21
    • 2014-09-25
    相关资源
    最近更新 更多