【问题标题】:Apply an 'or' to an inner joined where clause ActiveRecord将“或”应用于内部连接的 where 子句 ActiveRecord
【发布时间】:2015-05-20 11:55:38
【问题描述】:

型号:

class QueueList < ActiveRecord::Base
  has_many :times

  self.today
    joins(:times).where(times: { period_type: 'on', date: Date.today} || times: { period_type: 'from', date: Date.today }) 
  end
end

上述模型包含一个时间列表。因为 :times 可以是两种不同的 period_types,所以我必须对这个查询应用“OR”。有没有办法在不使用字符串查询的情况下完成上述错误代码,就像我在 ActiveRecord 中尝试做的那样?

【问题讨论】:

  • 现在,你会使用any_of gem

标签: ruby-on-rails ruby-on-rails-3 activerecord ruby-on-rails-3.2 rails-activerecord


【解决方案1】:

您可以使用 Arel 执行 OR 查询或使用 ActiverecordAnyOf gem。

但是,在您的情况下,您可以只使用包含(IN 语句)。例如:

.where(period_type: %w(on from), date: Date.today)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-08-17
    • 1970-01-01
    • 1970-01-01
    • 2011-12-02
    • 1970-01-01
    • 2023-03-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多