nolith / habtm-with-deferred-save

Rails plugin. Defers saving the records you add to habtm association until you call model.save, allowing validation in the style of normal attributes.

commit 3: bfd249c9ea1e
parent 2: 7abf2daec757
branch: default
git-svn-id: https://habtm-with-deferred-save.googlecode.com/svn/trunk@4 e8f5f159-813c-0410-8c33-cd1931e36101
tyle...@e8f5f159-813c-0410-8c33-cd1931e36101
3 years ago

Changed (Δ90 bytes):

Up to file-list has_and_belong_to_many_with_deferred_save/Readme:

4
4
5
5
Room has_and_belongs_to_many_with_deferred_save People
6
6
7
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.
7
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...
8
8
9
This isn't possible with the standard has_and_belongs_to_many due to these two problems:
9
This isn't possible using the standard has_and_belongs_to_many due to these two problems:
10
10
11
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.
11
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.
12
12
13
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...
13
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... 
14
14
15
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.
15
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.
16
16
17
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.
17
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.
18
18
19
19
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.).
20
20
21
21
The unsaved collection is automatically saved when you call save on the model.
22
22
23
24
23
==Project home
25
24
26
25
http://code.google.com/p/habtm-with-deferred-save/
27
26
28
29
27
==History
30
28
31
29
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).