基于多个条件的具有多个 where 条件的范围 [英] Scope with multiple where conditions based on multiple conditions
本文介绍了基于多个条件的具有多个 where 条件的范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否可以使用类似的东西
Is there possible to use something like
scope :state, ->(state) {
merge(where("start_time <= ? and end_time >= ?", Time.now.utc.beginning_of_day, Time.now.utc.beginning_of_day)) if state.include?("open")
merge(where("end_time < ?", Time.now.utc.beginning_of_day)) if state.include?("closed")
merge(where("start_time > ?", Time.now.utc.beginning_of_day)) if state.include?("upcoming")
}
如果我使用这个范围,只有最后一个是有效的.
If I use this scope, only the last one is functional.
例如:
- state(["upcoming"]) -> 工作
- state(["open"]) -> 未使用的地方
- state(["deleted"], ["upcoming"]) -> 仅在有即将到来的条件时使用
推荐答案
希望这能解决您的问题.
hope this solve your problem.
scope :state, ->(state) {where(self.query_conditions(state), q: Time.now.utc.beginning_of_day))}
def self.query_conditions(state)
q = ""
q+= "start_time <= :q and end_time >= :q" if state.include?("open")
q+= " and end_time < :q" if state.include?("closed")
q+= " and start_time > :q" if state.include?("upcoming")
q
end
这篇关于基于多个条件的具有多个 where 条件的范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文