Hi. I get a RecordNotSaved exception when trying to create a user record. The error: User Columns (0.005930) SHOW FIELDS FROM users SQL (0.000465) BEGIN SQL (0.000466) COMMIT ActiveRecord::RecordNotSaved (ActiveRecord::RecordNotSaved): ... As you can see, there''s no INSERT SQL generated, which is the root cause of the problem. In my user model, I have the following call back: def before_create self.password = Digest::SHA1.hexdigest(self.password) self.rsskey = Digest::SHA1.hexdigest(Time.now.to_s+"_"+String.rand(15)) self.otp = Digest::SHA1.hexdigest(Time.now.to_s+"_"+String.rand(15)) self.verified = false #logger.debug("--> "+self.to_s) #This is the magic line!! end The really really black magic part is that, if I comment the logging statement in, and thereby call self.to_s, the user record gets added. Can anyone come up with a somewhat rational reason for this behaviour? Thanks. Morten
Morten
2006-Jul-19 08:50 UTC
[Rails] Re: ActiveRecord::RecordNotSaved - bizarre behaviour.
It gets stranger yet.> #logger.debug("--> "+self.to_s) #This is the magic line!! > end > > The really really black magic part is that, if I comment the logging > statement in, and thereby call self.to_s, the user record gets added.I can log anything, eg. logger.debug("Hi"); and then things will work. It''s not the self.to_s that does something as I assumed. Morten
Rick Olson
2006-Jul-19 12:56 UTC
[Rails] Re: ActiveRecord::RecordNotSaved - bizarre behaviour.
On 7/19/06, Morten <lists@kikobu.com> wrote:> > It gets stranger yet. > > > #logger.debug("--> "+self.to_s) #This is the magic line!! > > end > > > > The really really black magic part is that, if I comment the logging > > statement in, and thereby call self.to_s, the user record gets added. > > I can log anything, eg. logger.debug("Hi"); and then things will work. > It''s not the self.to_s that does something as I assumed. > > Mortenreturning false on a before_* callback cancels the save operation: def before_create self.password = Digest::SHA1.hexdigest(self.password) self.rsskey = Digest::SHA1.hexdigest(Time.now.to_s+"_"+String.rand(15)) self.otp = Digest::SHA1.hexdigest(Time.now.to_s+"_"+String.rand(15)) self.verified = false # no, _this_ is the magic line!! #logger.debug("--> "+self.to_s) end -- Rick Olson http://techno-weenie.net
Morten
2006-Jul-19 15:29 UTC
[Rails] Re: ActiveRecord::RecordNotSaved - bizarre behaviour.
> > returning false on a before_* callback cancels the save operation: > > def before_create > self.password = Digest::SHA1.hexdigest(self.password) > self.rsskey = Digest::SHA1.hexdigest(Time.now.to_s+"_"+String.rand(15)) > self.otp = Digest::SHA1.hexdigest(Time.now.to_s+"_"+String.rand(15)) > self.verified = false # no, _this_ is the magic line!! > #logger.debug("--> "+self.to_s) > end >Oooh..! Thanks. Morten
Hampton
2006-Jul-19 16:47 UTC
[Rails] Re: ActiveRecord::RecordNotSaved - bizarre behaviour.
This had me SUPER confused. My .save methods were returning false and nothing was happening. I had no idea what was going on. Stupid assignments returning the value! Thanks Rick. -hampton. On 7/19/06, Rick Olson <technoweenie@gmail.com> wrote:> returning false on a before_* callback cancels the save operation: > > def before_create > self.password = Digest::SHA1.hexdigest(self.password) > self.rsskey = Digest::SHA1.hexdigest(Time.now.to_s+"_"+String.rand(15)) > self.otp = Digest::SHA1.hexdigest(Time.now.to_s+"_"+String.rand(15)) > self.verified = false # no, _this_ is the magic line!! > #logger.debug("--> "+self.to_s) > end > > -- > Rick Olson > http://techno-weenie.net > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Jeremy Kemper
2006-Jul-19 21:33 UTC
[Rails] ActiveRecord::RecordNotSaved - bizarre behaviour.
On Jul 19, 2006, at 1:44 AM, Morten wrote:> In my user model, I have the following call back: > > def before_create > self.password = Digest::SHA1.hexdigest(self.password) > self.rsskey = Digest::SHA1.hexdigest(Time.now.to_s > +"_"+String.rand(15)) > self.otp = Digest::SHA1.hexdigest(Time.now.to_s+"_"+String.rand > (15)) > self.verified = false > #logger.debug("--> "+self.to_s) #This is the magic line!! > end > > > The really really black magic part is that, if I comment the > logging statement in, and thereby call self.to_s, the user record > gets added. > > Can anyone come up with a somewhat rational reason for this behaviour?The before_create callback is returning false, which means you want to abort the save. At the end of your callback, put # Indicate that we want to proceed with the save return true jeremy