Sergio Ruiz
2011-Apr-28 19:06 UTC
[rspec-users] difference between validate and validate_on_create
i just ran into a problem with some test, and i am not exactly sure why, but the difference happened when i changed my model validation from: validate :custom_validation to: validate_on_create :custom_validation can someone give me any ideas on where in the chain the validation takes place? thanks! -- Posted via http://www.ruby-forum.com/.
Justin Ko
2011-Apr-28 19:57 UTC
[rspec-users] difference between validate and validate_on_create
On Thu, Apr 28, 2011 at 2:06 PM, Sergio Ruiz <lists at ruby-forum.com> wrote:> i just ran into a problem with some test, and i am not exactly sure why, > but the difference happened when i changed my model validation from: > > validate :custom_validation > > to: > > validate_on_create :custom_validation > > can someone give me any ideas on where in the chain the validation takes > place? > > thanks! > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >`validate` will run that validation on create *and* update. `validate_on_create` will only run on creation. -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20110428/b752116c/attachment.html>
Rick DeNatale
2011-Apr-28 19:57 UTC
[rspec-users] difference between validate and validate_on_create
On Thu, Apr 28, 2011 at 3:06 PM, Sergio Ruiz <lists at ruby-forum.com> wrote:> i just ran into a problem with some test, and i am not exactly sure why, > but the difference happened when i changed my model validation from: > > validate :custom_validation > > to: > > validate_on_create :custom_validation > > can someone give me any ideas on where in the chain the validation takes > place?This is really a Rails or ActiveRecord question rather than RSpec. In any case. validate :custom_validation will run the custom_validation method whether the model is being created or updated. validate_on_create :custom_validation will only run the custom_validation method when the method is being created. There''s also a validate_on_update method which will run the custom validation method when the model is being updated but not when it is created. http://guides.rubyonrails.org/active_record_validations_callbacks.html#creating-custom-validation-methods -- Rick DeNatale Blog: http://talklikeaduck.denhaven2.com/ Github: http://github.com/rubyredrick Twitter: @RickDeNatale WWR: http://www.workingwithrails.com/person/9021-rick-denatale LinkedIn: http://www.linkedin.com/in/rickdenatale