Rails ActiveRecord: is it possible to combine: include and: conditions?

Imagine I have wiki articles with many versions. I would like to query ActiveRecord through a database that only returns articles that have revisions that have been updated in the last 24 hours. Is it possible?

I would suggest that it would be something like:

Articles.find_all(:include => :revisions, :conditions => {:revision[updated_at] => 1.week.ago..Time.now.utc}) 

If this is not possible, is there some raw SQL code that I could pass find_by_SQL that would do the trick?

+6
include ruby-on-rails activerecord condition findall
source share
2 answers

If you need only an article and you do not need a full download of all the changes, you can use: instead, join It uses less memory because it does not preload all versions for each article. Use: include, although if you do something like article.revisions .

 Article.find(:all, :joins => :revisions, :conditions => ["revisions.updated_at > ?", 24.hours.ago] ) 
+4
source share

Depending on how you named your models, it will be something like this.

 Article.find(:all, :include => :revisions, :conditions => ["revisions.updated_at > ?", 1.week.ago]) 
+3
source share

All Articles