I am updating a collection of records like this (from the controller) def save_edits_for_all_teachers if Teacher.update(params[:teacher].keys, params[:teacher].values) flash[:notice] = "You have updated the teachers." redirect_to :controller => "/main_admin", :action => "menu" else render(:action => ''show_all_teachers'') and return end end In my Teacher model, I have a validate_on_update method that looks like this: def validate_on_update if lastname == "" errors.add(:lastname, "cannot be blank") end end However, it does not seem to be called. If I leave out the lastname of one of the teachers, the update continues and I am not returned to the page where the error occurred. Any idea why the validation does not get called? Does this have something to do with the fact that this is a collection being updated? What I would really like is to do validates_presence_of on both save and update. As I read the docs, though, it is one or the other, right? Shelby
Just put the validation in the standard validate() method. validate() gets called whenever save() does, so your object will be validated before the changes are saved to the DB. On 7/28/05, Shelby Westman <shelby.westman-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I am updating a collection of records like this (from the controller) > > def save_edits_for_all_teachers > if Teacher.update(params[:teacher].keys, params[:teacher].values) > flash[:notice] = "You have updated the teachers." > redirect_to :controller => "/main_admin", :action => "menu" > else > render(:action => ''show_all_teachers'') and return > end > end > > In my Teacher model, I have a validate_on_update method that looks like this: > def validate_on_update > if lastname == "" > errors.add(:lastname, "cannot be blank") > end > end > > However, it does not seem to be called. If I leave out the lastname > of one of the teachers, the update continues and I am not returned to > the page where the error occurred. Any idea why the validation does > not get called? Does this have something to do with the fact that > this is a collection being updated? > > What I would really like is to do validates_presence_of on both save > and update. As I read the docs, though, it is one or the other, > right? > > Shelby > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
No, sorry, that doesn''t work. As far as I can understand, I have to use the .update method as shown below in order to update a collection of objects from a view. And that update method doesn''t call the validate methods at all, it seems. (although I don''t know why...) Shelby On 7/28/05, Pat Maddox <pergesu-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Just put the validation in the standard validate() method. validate() > gets called whenever save() does, so your object will be validated > before the changes are saved to the DB. > > > > On 7/28/05, Shelby Westman <shelby.westman-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > I am updating a collection of records like this (from the controller) > > > > def save_edits_for_all_teachers > > if Teacher.update(params[:teacher].keys, params[:teacher].values) > > flash[:notice] = "You have updated the teachers." > > redirect_to :controller => "/main_admin", :action => "menu" > > else > > render(:action => ''show_all_teachers'') and return > > end > > end > > > > In my Teacher model, I have a validate_on_update method that looks like this: > > def validate_on_update > > if lastname == "" > > errors.add(:lastname, "cannot be blank") > > end > > end > > > > However, it does not seem to be called. If I leave out the lastname > > of one of the teachers, the update continues and I am not returned to > > the page where the error occurred. Any idea why the validation does > > not get called? Does this have something to do with the fact that > > this is a collection being updated? > > > > What I would really like is to do validates_presence_of on both save > > and update. As I read the docs, though, it is one or the other, > > right? > > > > Shelby > > _______________________________________________ > > Rails mailing list > > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
OK, the validation good news and bad news. The good news is that I can get a validation helper to work on both create and update by doing this: validates_presence_of :lastname, :on => :create validates_presence_of :lastname, :on => :update in my model. The bad news is that validation does not seem to be called as far as I can determine when a collection is being update with this code from a controller: if Teacher.update(params[:teacher].keys, params[:teacher].values) flash[:notice] = "You have updated the teachers." redirect_to :controller => "/main_admin", :action => "menu" else render(:action => ''show_all_teachers'') and return end Can anyone tell me if they have gotten validation to work when a collection is being updated rather than just a single model instance? Shelby