Rails ActiveRecord:是否可以组合 :include 和 :conditions 查询?
想象一下我有 wiki 文章,有很多修订。我想通过数据库使用 ActiveRecord 进行查询,该查询仅返回那些在过去 24 小时内更新过修订的文章。这样的事可能吗?
我想它会是这样的:
Articles.find_all(:include => :revisions,
:conditions => {:revision[updated_at] => 1.week.ago..Time.now.utc})
如果这不可能,是否有某种类型的原始 SQL 我可以传递给 find_by_SQL 来实现这个目的?
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?
I'd imagine it'd be something like:
Articles.find_all(:include => :revisions,
:conditions => {:revision[updated_at] => 1.week.ago..Time.now.utc})
If this isn't possible, is there some type of raw SQL I could pass to find_by_SQL that'd do the trick?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您只需要文章而不需要立即加载所有修订版,则可以使用 :joins 代替。它使用较少的内存,因为它不会预加载每篇文章的所有修订。如果您要执行类似
article.revisions
之类的操作,请使用 :include 。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
.根据您对模型的具体命名方式,它会是这样的。
Depending on how exactly you have your models named, it's going to be something like this.