[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Rails: Problems updating database entries

Depili

1/13/2006 3:03:00 PM

For some reason my code refuses to update the database entries and
doesn't even give any errors, the code is as follows:

def update
@team=Team.find(params[:id])
if @team.update_attributes(params[:team])
flash[:notice] = @team.attributes
end
end

The flash message displays all the fields with up-to-date values, but
nothing gets updated on the mysql database, also no errors are
produced.

I have tried using @team.attributes=(params[:team]) and @team.update
but with same results. elsewhere I have been using @team.update without
problems (in the create method, as I need the id to calculate some
fields).

Any ideas what might cause the update to fail without any errors?

7 Answers

richieb@gmail.com

1/13/2006 6:15:00 PM

0


Depili wrote:
> For some reason my code refuses to update the database entries and
> doesn't even give any errors, the code is as follows:
>
> def update
> @team=Team.find(params[:id])
> if @team.update_attributes(params[:team])
> flash[:notice] = @team.attributes
> end
> end
>
> The flash message displays all the fields with up-to-date values, but
> nothing gets updated on the mysql database, also no errors are
> produced.
>
> I have tried using @team.attributes=(params[:team]) and @team.update
> but with same results. elsewhere I have been using @team.update without
> problems (in the create method, as I need the id to calculate some
> fields).
>
> Any ideas what might cause the update to fail without any errors?

Don't you have to call @team.save ?

....richie

Depili

1/13/2006 10:47:00 PM

0

well, save will just return bunch of errors, because it tries to create
a new record with same id etc, also the scaffold code uses only
..update_parameters and the API says "update the parameter in object
provided by the hash and save it."

So I'm quite lost on this one..

It appears that the update method created by script/generate scaffold
Foo also fails, so there might be something wrong with my db (mysql)
but then again creating a new team with team.new, saving it and then
altering some data and calling team.update works, so why not team.find
-> alter data -> team.update...:/

Depili

1/13/2006 10:51:00 PM

0

The team.save also seems to do nothing, but it _should_ give a bunch of
errors, as using the .save method should try to save a duplicate entry,
leading to id conflicts.

the scaffold code uses only .update_parameters and the API says
sometihng like this (too lazy to copy-paste exact words) "update the
parameter in object provided by the hash and save it."

So I'm quite lost on this one..

It appears that the update method created by script/generate scaffold
Foo also fails, so there might be something wrong with my db (mysql)
but then again creating a new team with team.new, saving it and then
altering some data and calling team.update works, so why not team.find
-> alter data -> team.update...:/

Depili

1/13/2006 10:58:00 PM

0

Here's the accual code I use, which does nothing apart from outputting
the correct updated data into the flash notice:

def update
@team = Team.find(params[:id])
if @team.update_attributes(params[:team])
flash[:notice] = @team.attributes
@team.save
@team.update
redirect_to :action => 'show', :id => @team.Id
else
render :action => 'edit', :id => @team.Id
end
end

By all logic that should atleast give some errors...

Depili

1/14/2006 11:57:00 AM

0

Hmm, a freshly created table and scaffold code updates the records just
fine :/

Depili

1/14/2006 12:03:00 PM

0

Hmm, a freshly created database with scaffolding code is editable, but
for some reason my older database isn't with the same code.

Also for some reason @team doesn't by default give @team.id, I have
made some alterations to the database since running the initial
generate scaffold, is there something that I need to update?

Depili

1/14/2006 12:07:00 PM

0

Bingo, moving the id to the first field and restarting the server did
the trick :)