# HG changeset patch # User tyler.rick # Date 1192429873 0 # Node ID bfd249c9ea1e1e86ed850cc4256712e3999ae5a3 # Parent 7abf2daec7572e3a46030884a1b5d8d80b8152f3 git-svn-id: https://habtm-with-deferred-save.googlecode.com/svn/trunk@4 e8f5f159-813c-0410-8c33-cd1931e36101 diff -r 7abf2daec7572e3a46030884a1b5d8d80b8152f3 -r bfd249c9ea1e1e86ed850cc4256712e3999ae5a3 has_and_belong_to_many_with_deferred_save/Readme --- a/has_and_belong_to_many_with_deferred_save/Readme Mon Oct 15 06:18:27 2007 +0000 +++ b/has_and_belong_to_many_with_deferred_save/Readme Mon Oct 15 06:31:13 2007 +0000 @@ -4,28 +4,26 @@ Room has_and_belongs_to_many_with_deferred_save People -Lets say you want to validate your "has_and_belongs_to_many collection attribute" (Room.people in this example) so that it limits the number of objects in the collection to some maximum. +Lets say you want to validate the room.people collection and prevent the user from adding more people to the room than will fit. If they do try to add more people than will fit, you want to display a nice error message on the page and let them try again... -This isn't possible with the standard has_and_belongs_to_many due to these two problems: +This isn't possible using the standard has_and_belongs_to_many due to these two problems: -1. When I do the assignment to my collection (room.people = whatever), it IMMEDIATELY saves it in my join table (people_rooms) rather than waiting until I call room.save. +1. When I do the assignment to my collection (room.people = whatever), it immediately saves it in my join table (people_rooms) rather than waiting until I call room.save. -2. You can "validate" using habtm's :before_add option ... but it seems that any errors added there end up being ignored/lost. Plus, the only way to abort the save seems to be to raise an exception... +2. You can "validate" using habtm's :before_add option ... but it any errors added there end up being ignored/lost. The only way to abort the save from a before_add seems to be to raise an exception... -I don't want to raise an exception when the user violates my validation. I want validation of the people collection to be handled the same as any other field in the Room model: I want it to simply add an error to the Room model's error array which we can than display on the form with the other input errors. +We don't want to raise an exception when the user violates my validation. We want validation of the people collection to be handled the same as any other field in the Room model: We want it to simply add an error to the Room model's error array which we can than display on the form with the other input errors. -has_and_belongs_to_many_with_deferred_save solves this problem by overriding the setter method for your collection (people=), causing it to store the new members in a temporary variable rather than saving it immediately. +has_and_belongs_to_many_with_deferred_save solves this problem by overriding the setter method for your collection (people=), causing it to store the new members in a temporary variable (unsaved_people rather than saving it immediately. You can then validate the unsaved collection as you would any other attribute, adding to self.errors if something is invalid about the collection (too many members, etc.). The unsaved collection is automatically saved when you call save on the model. - ==Project home http://code.google.com/p/habtm-with-deferred-save/ - ==History It started as a post to the Rails mailing list asking how to validate a has_and_belongs_to_many collection/association (see http://www.ruby-forum.com/topic/81095).