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. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
On 9/3/06, dasil003 <gabriel.d-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > > 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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
Reasonably Related Threads
- question about saving associations when using has_many :through
- dependent support for has_many through?
- ActiveRecord delete_all With Sanitized Parameters?
- Review Request for Significant Performance Improvement in ActiveRecord
- delete_all not resetting auto incement in database