So, I''m attempting to make sure that the associated user_id of an object cannot be changed on update and I''m doing this with a before_update callback and this method in the model: before_update :lock_user_id def lock_user_id if self.user_id_changed? self.user_id = self.user_id_was unless self.user_id_was == nil end true end but this spec fails: it "user_id cannot be changed once QR has been created" do @qr.update_attributes(:user_id => -1) @qr.user_id.should == @user.id end at first I thought it was that update_attributes did not do a complete save (not that i''ve ever enabled partial saves), but this alternate spec also fails: it "user_id cannot be changed once QR has been created" do @qr.user_id = 99 @qr.save @qr.user_id.should == @user.id end even stranger is that a method in another model which does the same thing for another attribute passes its test. Anybody have any idea what i''m doing wrong/alternate suggestions? Thanks. -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Thu, Nov 4, 2010 at 6:30 AM, Gabriel Saravia <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org>wrote:> So, I''m attempting to make sure that the associated user_id of an object > cannot be changed on update and I''m doing this with a before_update > callback and this method in the model: > > before_update :lock_user_id > > def lock_user_id > if self.user_id_changed? > self.user_id = self.user_id_was unless self.user_id_was == nil > end > true > end > > but this spec fails: > > it "user_id cannot be changed once QR has been created" do > @qr.update_attributes(:user_id => -1) > @qr.user_id.should == @user.id > end > > at first I thought it was that update_attributes did not do a complete > save (not that i''ve ever enabled partial saves), but this alternate spec > also fails: > > it "user_id cannot be changed once QR has been created" do > @qr.user_id = 99 > @qr.save > @qr.user_id.should == @user.id > end > > even stranger is that a method in another model which does the same > thing for another attribute passes its test. > > Anybody have any idea what i''m doing wrong/alternate suggestions? > > Thanks.Only thing I could think of is validations for QR did not pass, which means that the before_update callback doesn''t get to be called at all. -- Erol M. Fornoles http://github.com/Erol http://twitter.com/erolfornoles http://ph.linkedin.com/in/erolfornoles -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Change @qr.update_attributes to @qr.update_attributes!, and I''ll bet you see that @qr failed validations and never saved. I make it a habit to always use update_attributes! in tests, so that a failure raises an exception. - D -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
You guys were absolutely right, it wasn''t passing validations and was not saved. Using update_attributes! demonstrated that, and I''m making it a point to remember that. Thanks to both of you! -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.