使用范围仅显示具有关联对象的项目
我正在使用 meta_search 来过滤汽车列表。汽车模型的定义如下:
class Car < ActiveRecord::Base
has_many :images, :class_name => "CarImage"
......
end
我想定义一个可与meta_search一起使用的范围,并仅返回至少具有图像的汽车。我可以让它工作的唯一方法是在范围内编写一个联接,但这似乎会返回重复的汽车对象:
scope :with_images, joins(:images)
如果我尝试仅选择不同的值,如下所示:
scope :with_images, joins(:images).select('DISTINCT cars.*')
...我搞乱了依赖于的其他方法我的搜索结果(例如带有组子句的平均值和计数聚合器)。
在浏览 Ruby on Rails 指南时,我发现了一个奇怪的示例,它可能会解决我的问题问题,但无法让它工作,而且我真的不明白它是如何完成的:
scope :published_and_commented, published.and(self.arel_table[:comments_count].gt(0))
如果我使用 self.arel_table[:images_count] ,那么它会说它是一个 nil 对象。
在整个应用程序中,我们在使用关联表中的属性进行搜索和过滤方面确实遇到了问题。我们通过简单地将属性移动到主 cars
表中解决了一些问题,但这有点愚蠢,尽管它执行得很快。
经过思考后,我认为可行的是,关联首先获取当前搜索中至少有一个图像的所有汽车 id 的列表(这可以通过简单的 INNER JOIN 来完成
和 DISTINCT id
查询),然后使用 id 列表来获取正常的列表查询,但使用 WHERE id IN [id_1, id_2, id_3, .. 。 , id_n]
子句。
您觉得这合理吗?我该怎么做呢? :D
另外,如果我不用 SQL,只用 Arel 或 MetaWhere 就能做到这一点,那就太好了...
I am using meta_search to filter through a listing of cars. The car model is defined like this:
class Car < ActiveRecord::Base
has_many :images, :class_name => "CarImage"
......
end
I want to define a scope that can be used with meta_search, and to return only the cars that have at least an image. The only way I could make it work is to write a join inside the scope, but that seems to return duplicate car objects:
scope :with_images, joins(:images)
If I try to select only distinct values, like this:
scope :with_images, joins(:images).select('DISTINCT cars.*')
...I mess up the other methods that rely on my search results (such as avg and count aggregators, with group clauses).
While browsing through the Ruby on Rails Guides, I found a curious example, that might solve my problem, but can't get it to work and I don't really understand how it's done:
scope :published_and_commented, published.and(self.arel_table[:comments_count].gt(0))
If I use self.arel_table[:images_count]
then it says it's a nil object.
Throughout the application, we really have problems with searching and filtering using attributes from the association tables. We solved some problems by simply moving the attribute to the main cars
table, but this is kind of stupid, though it performs fast.
After giving it a thought, what would be viable in my opinion is, that the association would first get the list of all cars ids in the current search that have at least one image (this could be done with a simple INNER JOIN
and DISTINCT id
query) and then use the list of id-s to get the normal listing query, but with a WHERE id IN [id_1, id_2, id_3, ... , id_n]
clause.
Would that seem reasonable to you? And how would I do that? :D
Also, would be nice if I could do it without SQL, just with Arel or MetaWhere...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您在 Rals 指南中找到的是最简单甚至最快的方法。
comments_count
是一个 counter_cache htat,您可以在关联上设置以获取子项的数量。轻松实现:
然后您将能够访问之前不存在的
images_count
。参见文档。
What you found in the Rals guides is the easiest way to proceed and even the fastest.
comments_count
is a counter_cache htat you can set on associations to get the number of children.Implement it easily:
And then you'll be able to access the previous non existing
images_count
.See doc.