Unfortunately, declaring my scope with the "scope" method instead of a
regular method returning an ActiveRecord::Relation doesn't solve my
problem. (it also creates a new one since "2.days.ago" is only evaluated
when the app loads the classes and not every time the method is called)
I think the includes method should support relations as well as
associations (since associations/scopes/relations are so well integrated
with eachother in rails 3).
Unfortunately, declaring my scope with the "scope" method instead of a
regular method returning an ActiveRecord::Relation doesn't solve my
problem. (it also creates a new one since "2.days.ago" is only evaluated
when the app loads the classes and not every time the method is called)
He made a mistake. It should be:
scope :recent, lambda { where ["published_at >= ?", 2.days.ago] }
I think the includes method should support relations as well as
associations (since associations/scopes/relations are so well integrated
with eachother in rails 3).
That way the following :
Hm, though that won't do the lazy eval. I checked the code for
has_many. The finder_sql is constructed the old way, not Arel all the
way down.
The compromise I can think of is to extend the collection code so it
loads all the comments in one batch and insert them into your
articles. The other one I can think of is to use AJAX to load the
recent comments and have a dedicated controller handle that. It's
probably a better design than trying to load a set of articles and
recent comments for all of them, in a single go, then rendering them
to the page.