Forum: Ruby on Rails Active Record save doesn't save! (or throw an exception)

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
D4b246038154d7cc2363256bd25a4fe0?d=identicon&s=25 Larry White (Guest)
on 2006-04-28 20:33
(Received via mailing list)
PLEASE help me - this is making me insane.

Can anyone suggest any reasons for this behavior?  It occured
previously,
then went away and is now back.

In Task.rb (where task is an active record subclass)

    def clear_schedule
        puts "clearing schedule for task"
        scheduled_end_date= nil                          #reset the
scheduled end date
        save
        halt.                                                 #this
makes
execution stop with an error.
    end


This is the console output this produces:

clearing schedule for task
127.0.0.1 - - [28/Apr/2006:13:44:01 Eastern Daylight Time] "POST
/tasks/update/1
06 HTTP/1.1" 500 10736
http://localhost:3000/tasks/edit/106 -> /tasks/update/106

When I query the db, scheduled_end_date is still set to a valid date,
even
though in the model I set it to nil, called save and then stopped.
There
were no exceptions other than this:

undefined local variable or method `halt' for #<Task:0x3a23328>

I sometimes have the opposite problem in a related piece of code for
this
same object/column (I set it to a real value, save it, retrieve the
object
from the database in the very next line of code. When I check the value
it's
nil.)

Can someone tell me what the expected behavior is within a transaction
versus outside of a transaction?  The behavior seems unpredictable in
both
situations but I'm not sure.  I assumed that if I was inside a
transaction
that I would still see the new value.  All of this occurs within a
single
http request so I assumed that all the data access was on the same
connection.

Anyway, I'm stumped and resolving this is critical to my project.
Please
help if you can.
3ec705c5dd3480c6268b72c5617e8dae?d=identicon&s=25 Michael Smedberg (Guest)
on 2006-04-28 20:40
(Received via mailing list)
You might want to use the "save!" method, instead of "save".  The former
will throw an exception if there are problems, the latter will simply
return
false.  You can catch the exception and look at it to see what's really
going on.
D4b246038154d7cc2363256bd25a4fe0?d=identicon&s=25 Larry White (Guest)
on 2006-04-28 20:49
(Received via mailing list)
I just tried that - (also wrapped the call with an if )

     if (save!)
      puts "I SAVED IT with !!!!"
    else
      puts "THE SAVE FAILED"
    end

It outputs "I SAVED IT ...." to the console but still the db record
contains
a real value not nil when the program stops on the next line.
59de94a56fd2c198f33d9515d1c05961?d=identicon&s=25 Tom Mornini (Guest)
on 2006-04-28 20:55
(Received via mailing list)
On Apr 28, 2006, at 11:32 AM, Larry White wrote:

> scheduled end date
>         save
>         halt.                                                 #this
> makes execution stop with an error.

change halt to

breakpoint(self)

>     end

and then type:

   script/breakpointer

before hitting the page. It will show an "error" that said there's
nothing to breakpoint, which is normal, because no code is in a
breakpoint yet.

When you hit the page, the browser will freeze, and the breakpointer
will come alive, and show you the Task object.

Take a look at the @errors attribute.

--
-- Tom Mornini
D4b246038154d7cc2363256bd25a4fe0?d=identicon&s=25 Larry White (Guest)
on 2006-04-28 21:04
(Received via mailing list)
Thank you.  I tried that and got @erros={}

but I just looked In my Postgres logs and noticed that I'm getting this
error message -

Unexpected EOF on client connection.
3ec705c5dd3480c6268b72c5617e8dae?d=identicon&s=25 Michael Smedberg (Guest)
on 2006-04-28 21:07
(Received via mailing list)
OK, so it sounds like the save is happening successfully.  Do you know
what
it's doing?  I'd look at the log file, to see what SQL is happening.  If
there's no SQL changing the data, then the problem is with your Ruby
code (
e.g. maybe you overrode the "scheduled_end_date=" method or something.)
If
the SQL is being run, then check the DB- make sure the SQL is successful
(though it's not obvious why it wouldn't be.)  If the data is in the DB,
then the problem is with reading it, not with the save.

I know- these are just general suggestions, not a solution, but I hope
it's
a start...
D4b246038154d7cc2363256bd25a4fe0?d=identicon&s=25 Larry White (Guest)
on 2006-04-28 21:26
(Received via mailing list)
The write is not happening at all. I've got optimistic locking on and
the
lock-version field did not update so the problem isn't with an
individual
setter (though I had that problem with this same code a couple months
ago. I
assumed originally that it was back.).
D4b246038154d7cc2363256bd25a4fe0?d=identicon&s=25 Larry White (Guest)
on 2006-04-28 21:47
(Received via mailing list)
My mistake - i was looking at the wrong task record.  The record is
getting
saved, but the scheduled_end_date field is not getting updated.   Since
my
task object doesn't have scheduled_end_date over-written (any more),
what
else could cause that?  Something weird seems to be going on in the db
mapping.

Does anyone know if there are any known issues supporting the Postgres
timestamptz type?)
This topic is locked and can not be replied to.