简体   繁体   English

如何使用Rails作用域或where子句查询OR AND条件

[英]How to use rails scope or where clause to query OR AND conditions

so I'm trying to make queries that look like this 所以我正在尝试进行如下查询

Car Model
has_many :colors
scope :for_cars, ->(color) { Car.join(:color).where(colors: { name: color}) }

for cars of green OR blue i can do this. 对于greenblue汽车,我可以做到这一点。

Car.for_cars(['green', 'blue'])

and I want to get cars that are green AND blue , how do i do that? 我想得到greenblue汽车,我该怎么做?

I'm using postgres. 我正在使用postgres。

Query in this way:- 以这种方式查询:

scope :for_cars, ->(color) { Car.joins(:colors).where("colors.name" =>  color) }

Or in this way much simpler:- 或者 in this way much simpler:-

scope :for_cars, ->(color) { Car.joins(:colors).where("colors.name":  color) }

Or in this way also if this scope is in Car model:- 或者 in this way also if this scope is in Car model:-

scope :for_cars, ->(color) { joins(:colors).where("colors.name":  color) }

Note: - Tested in Rails 4.2.9 注意: - Tested in Rails 4.2.9

Check the Rails guides on joins 查看关于联接Rails指南

Car.for_cars('green').merge(Car.for_cars('blue'))

This will generate a single query with two joins and return intersection. 这将生成具有两个联接并返回交集的单个查询。 https://apidock.com/rails/v4.2.7/ActiveRecord/SpawnMethods/merge https://apidock.com/rails/v4.2.7/ActiveRecord/SpawnMethods/merge

But this approach gets ineffective as the number of desired colors grows (too many joins). 但是,随着所需颜色数量的增加(连接过多),此方法将无效。

You can also do something like this: 您还可以执行以下操作:

wanted_colors = ['green', 'blue', 'red', 'yellow']

Car.for_cars(wanted_colors).group("cars.id").having("COUNT(colors.id) = #{wanted_colors.size}")

This approuch might defer depending on RDBMS you use. 根据您使用的RDBMS,此方法可能会延迟。

One way to achieve this, without a scope, would be to have methods on Car: 在没有范围的情况下,实现此目标的一种方法是在Car上使用方法:

def color_names
  colors.map(&:name)
end

def has_all_color_names(has_color_names = [])
  (color_names & has_color_names).size == has_color_names.size
end

def self.has_all_color_names(has_color_names = [])
  includes(:colors).select { |car| car.has_all_color_names has_color_names }
end

... and then to ... ...然后...

Car.has_all_color_names %w(Green Blue)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 SQL查询-如何在WHERE子句中放置条件 - SQL query - how to put conditions in WHERE clause 如何在SQL的WHERE子句中动态使用条件 - How to use conditions dynamically in WHERE clause in SQL SQL:如何在带有“ NOT IN”条件的“ Where”子句中使用“ and”和“ or” - SQL : How to use “and” and “or” in a “Where” clause with “NOT IN” conditions 如何在 SQL Where 子句中使用多个 And 条件 - How to use multiple And conditions in SQL Where clause LARAVEL 查询生成器:如何将 WHERE 子句中的 AND 和 OR 条件与多个条件结合起来 - LARAVEL Query Builder: How to combine AND and OR conditions in WHERE clause with multiple conditions 如何在WHERE子句中使用query()? - How use query() with WHERE clause? Rails/SQL 结合了 rails 范围的 where 条件 - Rails/SQL combine where conditions for rails scope 如何使用 where 子句和 OR 条件进行 X++ 查询 - How to make X++ query with where clause and OR conditions SQLite的Where子句中有多个条件时如何使用“或”语句? - How to use the "Or" statement when having multiple conditions in the Where clause in SQLite? 如何在SQL查询的where子句中使用If else - How to use If else in where clause of sql query
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM