[英]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. 对于
green
或blue
汽车,我可以做到这一点。
Car.for_cars(['green', 'blue'])
and I want to get cars that are green
AND blue
, how do i do that? 我想得到
green
和blue
汽车,我该怎么做?
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.