"YOU AND THE ART OF ONLINE DATING" is the only product on the market that will take you step-by-step through the process of online dating, provide you with the resources to help ensure success. Get it now!
You had two problems here: You're overriding validations The order of operations in saving is causing problems You're overwriting the validate method which is a bad thing, because the built in behaviour to deny records with validation errors to be saved to the database. To add custom validations you want to do this: validate :maximum_group_length def maximum_group_length if self.groups. Length > 5 self.errors.
Add(:groups, "cannot have more than 5 groups") end end However, the nature of HABTM relationships requires you to do it as an after_save callback. Just because of the order that things are done user. Groups is based on the implicit join table and there for isn't updated until the join table is updated If you're trying to validate as part of a callback (before_save, after_creation, etc.), then adding an error to the object won't trigger a roll back.
Callbacks will only trigger a rollback if they return false. This will handle the after save implementation the question suggest after_save :validate_maximum_group_length def validate_maximum_group_length if self.groups. Length > 5 self.errors.
Add(:groups, "cannot have more than 5 groups") return false end end Another solution is to use an explicit join model. And a has_many :through relationship. The join model's table is updated in the update statement.
Where as the has_many :through and HABTM relationships update the relationship after the save class User user_groups, allow_destroy validate :max_group_length errors. Add(:groups, "cannot have more than 5 groups") if self. User_groups.
Length > 5 end end class UserGroup.
You had two problems here: You're overriding validations The order of operations in saving is causing problems. You're overwriting the validate method which is a bad thing, because the built in behaviour to deny records with validation errors to be saved to the database. To add custom validations you want to do this: validate :maximum_group_length def maximum_group_length if self.groups.
Length > 5 self.errors. Add(:groups, "cannot have more than 5 groups") end end However, the nature of HABTM relationships requires you to do it as an after_save callback. Just because of the order that things are done.User.
Groups is based on the implicit join table and there for isn't updated until the join table is updated. If you're trying to validate as part of a callback (before_save, after_creation, etc. ), then adding an error to the object won't trigger a roll back. Callbacks will only trigger a rollback if they return false.
This will handle the after save implementation the question suggest. After_save :validate_maximum_group_length def validate_maximum_group_length if self.groups. Length > 5 self.errors.
Add(:groups, "cannot have more than 5 groups") return false end end Another solution is to use an explicit join model. And a has_many :through relationship. The join model's table is updated in the update statement.
Where as the has_many :through and HABTM relationships update the relationship after the save. Class User user_groups, allow_destroy validate :max_group_length errors. Add(:groups, "cannot have more than 5 groups") if self.
User_groups. Length > 5 end end class UserGroup However you will need to modify your form to update the form to supply group_id in the params hash as params:user:user_group_attributes0:group_id3.
This doesn't solve it, it's still persisting the changes. – jonnii Oct 19 '09 at 21:32 Looks like I misdiagnosed your problem, I thought you were validating as a callback. The problem was that you were redefining validate.
– EmFi Oct 19 '09 at 21:47 I've moved the validation into a custom validation routine as suggested, but this still doesn't solve the problem. – jonnii Oct 19 '09 at 21:48 Added a list of other possible causes to my solution. The previously posted code works.
– EmFi Oct 19 '09 at 22:33 Oops was testing with just a has_many relationship. Corrected and provided explanation of the HABTM problem. – EmFi Oct 19 '09 at 22:55.
The order of operations in saving is causing problems. You're overwriting the validate method which is a bad thing, because the built in behaviour to deny records with validation errors to be saved to the database. However, the nature of HABTM relationships requires you to do it as an after_save callback.
Just because of the order that things are done. Groups is based on the implicit join table and there for isn't updated until the join table is updated.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.