Object.save


#1

This is a general questions, but one that I have tested and think I know
the answer to but…

Lets say I have a table called Users and I create an object to store
data for a user in an object called @user. Now, I save that user info by
saying ‘@user.save’. Now, lets say later on I do a @user =
User.find_by_id( params[:id] ) which will have the same id for the
previous user. And then I say @user.save. If I’m understanding things
right, these actiosn will create only one user in the database since
they both have the same id’s, but the second save acts as an update? Is
this write.

The reason I ask is because I am making a form that allows a user to
create user information one step at a time. Then at the end all their
info is redisplayed and they ‘confirm’ that it is write and then any
changes that they made, or if they didn’t make any changes, are saved
back to the database. I just want to make sure that this will not create
multiple entries for the same user. It doesn’t to have done so, but hen
again I have not played with it that much. Thanks,

–Shandy


#2

Yes this should work fine. I think the only way you’ll get a new record
in the database is when you do @user = User.new and such.

Note that you can easily test this with script/console.


#3

def save
create_or_update
end

So, it should create or update an object

On Jul 20, 11:16 am, Shandy N. removed_email_address@domain.invalid


#4

Shandy N. wrote:

The reason I ask is because I am making a form that allows a user to
create user information one step at a time. Then at the end all their
info is redisplayed and they ‘confirm’ that it is write and then any
changes that they made, or if they didn’t make any changes, are saved
back to the database. I just want to make sure that this will not create
multiple entries for the same user. It doesn’t to have done so, but hen
again I have not played with it that much. Thanks,

–Shandy

I would do @user.update_attributes(params[:user])

~Jeremy