【发布时间】:2014-10-21 14:25:51
【问题描述】:
我的范围可以归结为以下内容
Expression.where(library_id:[1,2,3], account_id: [1337, nil])
当我使用where_values_hash 评估生成的 arel 关系时,我没有看到最后一项:
q = Expression.where(library_id:[1,2,3], account_id: [1337, nil])
q.where_values_hash #=> { "library_id"=>[1, 2, 3] }
这是因为where_values_hash limits its results to items of class Arel::Nodes::Equality。第二项是Arel::Nodes::Grouping:
q.where_values.second.class
#=> Arel::Nodes::Grouping
我想看看这个项目是否配置正确[1]
expect(q.where_values_hash).to include({"library_id" => [1,2,3]}) # Passes
expect(q.where_values_hash["account_id"]).to include(1337) # Fails
expect(q.where_values_hash["account_id"]).to include(nil) # Fails
如何以类似于阅读where_values_hash 的内容的方式阅读此“Arel::Nodes::Grouping”?
[1] 这是为了测试:我不想测试 ActiveRecord 是否有效:我相信它确实有效。我想知道在某些情况下,范围配置了正确的参数。
【问题讨论】:
-
我也遇到了这个问题。您是否找到解决或规避此问题的方法?在我的例子中,班级是
Arel::Nodes::In,我的查询类似于Post.where(blog: { name: 'hello world' }) -
我正在考虑只是在 RSpec 支持类中对 Relation 类进行猴子修补,并且仅在我需要的特定规范上才需要它
-
哦,看来in Rails 4.2 this is possible,至少对我来说是这样。
where_values_hash接受一个参数来指定它应该从哪个表中提取值。这至少为我解决了这个问题。
标签: ruby-on-rails activerecord arel