ActiveRecord::RecordNotSaved - bizarre behaviour

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

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

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

Oooh…! Thanks.

Morten

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.

using script/console can be a big help in these kind of things… [for
future ref]

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs