New Technique: Subsets of has_many Associations

I just now thought of this, and sure enough it works like a charm (at
least so far in my limited testing):

  has_many :events, :dependent => :delete_all
  has_many :upcoming_events, :class_name => "Event", :conditions =>
"date > NOW()"

The purpose of this is that it makes eager loading of subsets of
associations possible without replacing all the magic of the original
association that you may want. If you consider that the number of
events may grow to thousands, but we typically are only interested in
evens in the future, this makes the database queries quite a bit more
efficient. However, when you put conditions on an association like
that, it is enforced globally and suddenly things like :dependent =>
:delete_all will only delete events from the future as well.

I welcome feedback and potential gotchas in using this technique. I'm
also especially interested in documentation on prior uses of this
technique. I googled pretty extensively, but its hard to separate this
particular use from a lot of other issues people are talking about.

I have written a complete writeup on the thought process at:

http://darwinweb.net/article/Subsets_Of_Associations_In_Activerecord

So far I've tested it for basic eager loading (even of both
associations at the same time!) and it worked as expected. I also
tested the << operator, and it works.

If you add an :upcoming_event it doesn't check the conditions, so you
may have to reload. I don't consider that a bug since it would require
an extra database query, and the behaviour is not unexpected.

I just now thought of this, and sure enough it works like a charm (at
least so far in my limited testing):

has_many :events, :dependent => :delete_all
has_many :upcoming_events, :class_name => “Event”, :conditions =>

“date > NOW()”

The purpose of this is that it makes eager loading of subsets of
associations possible without replacing all the magic of the original
association that you may want. If you consider that the number of

events may grow to thousands, but we typically are only interested in
evens in the future, this makes the database queries quite a bit more
efficient. However, when you put conditions on an association like

that, it is enforced globally and suddenly things like :dependent =>
:delete_all will only delete events from the future as well.

I welcome feedback and potential gotchas in using this technique. I’m
also especially interested in documentation on prior uses of this

technique. I googled pretty extensively, but its hard to separate this
particular use from a lot of other issues people are talking about.

You may want to look at acts_as_view:

http://habtm.com/articles/2006/07/23/acts_as_view

I have written a complete writeup on the thought process at:

http://darwinweb.net/article/Subsets_Of_Associations_In_Activerecord

So far I’ve tested it for basic eager loading (even of both

associations at the same time!) and it worked as expected. I also
tested the << operator, and it works.

If you add an :upcoming_event it doesn’t check the conditions, so you
may have to reload. I don’t consider that a bug since it would require

an extra database query, and the behaviour is not unexpected.

Michael Guterl