Rails的ActiveRecord的:是一个组合:包括和:条件查询的可能吗? [英] Rails ActiveRecord: Is a combined :include and :conditions query possible?
问题描述
想象一下,我有维基文章,有许多修订。我想通过数据库,其中仅返回那些有它在过去24小时内被更新修订文章做ActiveRecord的查询。这样的事情可能吗?
Imagine I have wiki Articles, with many Revisions. I'd like to do a query with ActiveRecord through the database, which only returns those Articles which have Revisions which are updated in the past 24 hours. Is such a thing possible?
我想像它会是这样的:
Articles.find_all(:include => :revisions,
:conditions => {:revision[updated_at] => 1.week.ago..Time.now.utc})
如果这是不可能的,是有一些类型的原始SQL中我可以传递到的find_by_sql是会做的伎俩?
If this isn't possible, is there some type of raw SQL I could pass to find_by_SQL that'd do the trick?
推荐答案
如果您只需要文章,并不需要所有的修改渴望加载,你可以使用:连接代替。它使用较少的内存,因为它不是preloading所有修订每篇文章。用途:包括但如果你会做这样的事情 article.revisions
If you only need the article and don't need eager loading of all the revisions, you could use :joins instead. It uses less memory because it's not preloading all the revisions for each article. Use :include though if you will be doing something like article.revisions
.
Article.find(:all,
:joins => :revisions,
:conditions => ["revisions.updated_at > ?", 24.hours.ago]
)
这篇关于Rails的ActiveRecord的:是一个组合:包括和:条件查询的可能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!