smart eager loading and caching

I’d like to deprecate methods like includes and eager_load in Rails since I think it is possible to automatically detect when they are needed. Ideally the developer could know very little about how databases work and still get very efficient queries using just the ORM. What do other people think?

There’s a ton of magic in active record as is. If you have ideas on a big change like the one you are suggesting, you should fork active record and get it working to get feedback. After that it’s worth discussing.

If you really want to help here, take a look at open issues tagged with active record and see if you can solve them. AR has the most issues open by far.

I’m not terribly convinced that there’s a useful heuristic to automatically make the right choices for eager-loading, since the right associations to load are typically driven by what the code does with the results.

For instance, this code fragment should either use something different than `present?` or should use an eager-load, but the right choice depends on what happens after the records are loaded:

@posts.each do |post|
  # present? will cause the comments association to load
  if post.comments.present?
    # do something
    # if post.comments is referenced here, it should have been eager-loaded
    # if it isn’t referenced, the present? should really be changed to exists? or similar

Even if such a heuristic existed, it would still be wrong some of the time - so things like `includes` and `eager_load` would still be needed to specify the correct behavior in those cases.

It’s definitely worth discussing, but it’s going to (IMO) face a pretty steep climb to success.

—Matt Jones

I agree with Matt on this subject. Even if we could come up with useful heuristics, we would not deprecate includes, eager_load and preload. There are always situations you can’t detect. Also I wouldn’t want to use that feature on some of my apps. It can be crucial what Query is executed and such magic code would make it impossible to control.


– Yves