morten
July 19, 2006, 10:45am
1
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
July 19, 2006, 10:52am
2
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
morten
July 19, 2006, 2:59pm
3
On 7/19/06, Morten [email protected] wrote:
It’s not the self.to_s that does something as I assumed.
Morten
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
morten
July 19, 2006, 6:50pm
5
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.
morten
July 20, 2006, 10:48am
6
using script/console can be a big help in these kind of things… [for
future ref]
morten
July 19, 2006, 11:36pm
7
On Jul 19, 2006, at 1:44 AM, Morten wrote:
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