ActiveRecord: How to query multiple associations with an AND statement?

Nathan

So I have product with multiple features, and features belong_to a feature_key and a feature_value.

Here is the query I'm running:

Product.family_features.joins(:feature_key, :feature_value)
       .where(feature_keys: { name: ["Color", "Size"] }, feature_values: { name: ["Red", "Large"] })
       .group_by(&:product_id)

Note: .family_features is a scope on Product that gets all the features related to the product's "family" -- which is a group of other products.

Now I want only one product that has a combination of features that have "color" => "red" and "size" => "large"

But I get features that have "color" => "red" and other sizes, as well as features with "size" => "large" and other colors.

Is there a way to constrain this query to the exact combination of values I need?


The relations look like this:

Product has_many :features

Feature belongs_to :product, :feature_key, :feature_value

FeatureKey has_many :features, :products (through features)
FeatureValue has_many :features, :products (through features)
Daniel Perez

You should be able to do this using arel.

fkt, fvt = FeatureKey.arel_table, FeatureValue.arel_table color_red = fkt[:name].eq('Color').and(fvt[:name].eq('Red')) size_large = fkt[:name].eq('Size').and(fvt[:name].eq('Large')) Feature.joins(:feature_key, :feature_value) .where(color_red.or(size_large)) .group(:product_id)

I have not tested the code, but I think it should work. Check the arel documentation for more details.

Note that I also changed the group_by to group. group_by is the Enumerable module Ruby method, while group is the ActiveRecord relation method performing an SQL GROUP BY, which should be much faster than grouping in Ruby.

Edit: Though your comment is saying that it worked, if you need all products that have size => large and color => red combination, I do not think this was what you were looking for.

You could try something like this:

red_products = Product.joins(features: [:feature_key, :feature_value]).where({feature_keys: { name: 'color' }, feature_values: { name: 'red' }})
large_products = Product.joins(features: [:feature_key, :feature_value]).where({feature_keys: { name: 'size' }, feature_values: { name: 'large' }})
products = red_products & large_products

上述解决方案的问题&在于Ruby中的数组交集,因此它将进行两个数据库查询,并与Ruby中的结果相交,这可能会成为大型数据集的问题。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

合并Kinvey中的用户

来自分类Dev

PostgreSQL:有效地将JSON数组拆分为行

来自分类Dev

是否可以在UITextField中设置较大的空间

来自分类Dev

从git bisect错误中恢复更有针对性吗?

来自分类Dev

GIT SVN:在没有错误的合并父级的情况下获取重新创建的SVN分支

来自分类Dev

如何在核心数据模型iOS中找出特定属性索引的值

来自分类Dev

iOS-Xcode中的iPad预览丢失

来自分类Dev

Java:方法挂钩和查找对象实例

来自分类Dev

使用git diff,如何显示从索引到给定提交的补丁?

来自分类Dev

仅用于向上投射

来自分类Dev

如何将光标下的值乘以重复计数?

来自分类Dev

boost :: ublas如何获取int矩阵的行列式?

来自分类Dev

通过使用javascript原型系统创建共享结构的不可变对象是否有意义?

来自分类Dev

在另一个任务之前异步运行grunt任务

来自分类Dev

Bootstrap 3:滚动条

来自分类Dev

引导程序3:同一行上有两种形式

来自分类Dev

在编译时查找基类

来自分类Dev

RegionMonitoring监视的“区域”数,基于用户在iOS7中的当前位置

来自分类Dev

从一个ViewController传递到另一个ViewController的NSCache UIImage数据

来自分类Dev

Unable to find library in eclipse for Android

来自分类Dev

boost :: spirit :: qi匹配[]麻烦

来自分类Dev

可访问性不一致:与方法“ y”相比,参数类型“ x”的访问性较差

来自分类Dev

boost :: chrono :: system_clock-如何格式化当前时间到字符串?

来自分类Dev

考虑工厂功能和序列化的对象键入的推荐方法

来自分类Dev

Javascript中类似Haskell的类型签名有任何约定吗

来自分类Dev

如何使用JavaScript中的共享,不可变引用类型跟踪更改

来自分类Dev

使用Object.prototype.toString时有哪些极端情况?

来自分类Dev

索引中有缺口的数组是否带来了弥补其缺点的任何好处

来自分类Dev

如何实现更通用的reduce功能以允许提前退出?

Related 相关文章

  1. 1

    合并Kinvey中的用户

  2. 2

    PostgreSQL:有效地将JSON数组拆分为行

  3. 3

    是否可以在UITextField中设置较大的空间

  4. 4

    从git bisect错误中恢复更有针对性吗?

  5. 5

    GIT SVN:在没有错误的合并父级的情况下获取重新创建的SVN分支

  6. 6

    如何在核心数据模型iOS中找出特定属性索引的值

  7. 7

    iOS-Xcode中的iPad预览丢失

  8. 8

    Java:方法挂钩和查找对象实例

  9. 9

    使用git diff,如何显示从索引到给定提交的补丁?

  10. 10

    仅用于向上投射

  11. 11

    如何将光标下的值乘以重复计数?

  12. 12

    boost :: ublas如何获取int矩阵的行列式?

  13. 13

    通过使用javascript原型系统创建共享结构的不可变对象是否有意义?

  14. 14

    在另一个任务之前异步运行grunt任务

  15. 15

    Bootstrap 3:滚动条

  16. 16

    引导程序3:同一行上有两种形式

  17. 17

    在编译时查找基类

  18. 18

    RegionMonitoring监视的“区域”数,基于用户在iOS7中的当前位置

  19. 19

    从一个ViewController传递到另一个ViewController的NSCache UIImage数据

  20. 20

    Unable to find library in eclipse for Android

  21. 21

    boost :: spirit :: qi匹配[]麻烦

  22. 22

    可访问性不一致:与方法“ y”相比,参数类型“ x”的访问性较差

  23. 23

    boost :: chrono :: system_clock-如何格式化当前时间到字符串?

  24. 24

    考虑工厂功能和序列化的对象键入的推荐方法

  25. 25

    Javascript中类似Haskell的类型签名有任何约定吗

  26. 26

    如何使用JavaScript中的共享,不可变引用类型跟踪更改

  27. 27

    使用Object.prototype.toString时有哪些极端情况?

  28. 28

    索引中有缺口的数组是否带来了弥补其缺点的任何好处

  29. 29

    如何实现更通用的reduce功能以允许提前退出?

热门标签

归档