Forum: Ruby on Rails update_attributes, want to add not replace?

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.
Lars A. (Guest)
on 2007-06-17 09:15
Hi hi,
I am currently changing some things in an old Ruby on Rails application,
and I have run into a little problem. This is how the function at hand
looks like:

  def update_js
    @url = params[:webpage][:url]
    @webpage = Webpage.find(:first, :conditions => ["url = ?", @url])
    if @webpage == nil
      @webpage = Webpage.new(params[:webpage])
      @webpage.save
    else
      @webpage.update_attributes(params[:webpage])
    end
    render :text => "#{params[:callback]}(\"#{params[:id]}\");"
  end

The problem is
...
else
      @webpage.update_attributes(params[:webpage])
...

I dont want to replace all of the attributes when updating, I want to
insert some more values to the already existing. I've been trying to
rummage around in the API all morning, but hasn't got me anywhere. Very
grateful for any help,
Lars
shai (Guest)
on 2007-06-17 16:33
the params[:webpage] has a couple of fields that are 'updating' the
object.
all you need to do is find the param that u need to update. assign the
value, save it, and you're set.

i.e

say you have a form
<%= text_field 'webpage', 'foo' %>

in the controller do:

@webpage.attribute_you_want_to_update = params[:webpage][:foo]
@webpage.save

this sets the object's attribute value, without it affecting the other
attributes, and when you use the save method, you are writing it sql
wise.

:)

hth

shai
MichaelLatta (Guest)
on 2007-06-17 17:56
(Received via mailing list)
I think you need to more clearly state what you want.  This seems to
be a pretty straightforward create/update
piece of code.

Michael

On Jun 16, 10:15 pm, Lars A. <removed_email_address@domain.invalid>
Lars A. (Guest)
on 2007-06-20 07:31
Thanks for your help shai!
As you probably understand, I am a total newbie. If I understand it
correctly,
when you say

> in the controller do:
>
> @webpage.attribute_you_want_to_update = params[:webpage][:foo]
> @webpage.save

means that the specific attribute_I_want_to_update is replaced with the
new one, :foo. Am I assuming correctly?
However, my problem is that I wish to keep the old values of
attribute_I_want_to_update and then add the values of :foo. The
@webpage.attribute = params[:webpage][:foo] simply assigns a new value,
replacing the old one, right?
So grateful for your guys quick replies and help,
Lars
shai (Guest)
on 2007-06-20 11:36
> means that the specific attribute_I_want_to_update is replaced with the
> new one, :foo. Am I assuming correctly?

yes

> However, my problem is that I wish to keep the old values of
> attribute_I_want_to_update and then add the values of :foo. The
> @webpage.attribute = params[:webpage][:foo] simply assigns a new value,
> replacing the old one, right?

so do

@webpage.foo = @webpage.foo + params[:webpage][:foo]
# small note; make sure they are the same  datatype (i.e webpage.foo is
a string)
@webpage.save

that should save the old value as well as appending the new one.
glad to give help of any kind :)

enjoy
Lars A. (Guest)
on 2007-06-23 09:49
Thanks again! It works like a charm now, and I cant see how I couldnt
figure that one out for myself now. Thank you Shai!
/Lars

>
> so do
>
> @webpage.foo = @webpage.foo + params[:webpage][:foo]
> # small note; make sure they are the same  datatype (i.e webpage.foo is
> a string)
> @webpage.save
>
> that should save the old value as well as appending the new one.
> glad to give help of any kind :)
>
> enjoy
This topic is locked and can not be replied to.