Forum: Ruby on Rails Object.save

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.
C4bfcc81ac9281cb905f38e97e4d4e0b?d=identicon&s=25 Shandy Nantz (snantz)
on 2007-07-20 20:16
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
5e75f4b81923cd04a5c8fffa290be92e?d=identicon&s=25 Carl Johnson (carlivar)
on 2007-07-20 21:23
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.
2a4930e9650e669c9169e4d391b66ed3?d=identicon&s=25 Ashish (Guest)
on 2007-09-28 21:24
(Received via mailing list)
def save
  create_or_update
end

So, it should create or update an object

On Jul 20, 11:16 am, Shandy Nantz <rails-mailing-list@andreas-s.net>
899f97295621824a5f482a286f5062ca?d=identicon&s=25 Jeremy Woertink (nuby2ruby)
on 2007-09-29 00:33
Shandy Nantz 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
This topic is locked and can not be replied to.