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.
E6e0df436a7b752ccf182af73b05bc53?d=identicon&s=25 Lars Andren (pappa-gottis)
on 2007-06-17 07: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
4c3acdc3d93f54cc7a7281780ec8a4ee?d=identicon&s=25 shai (Guest)
on 2007-06-17 14: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
588ab1c0a5610a7e160a3b101abb91e6?d=identicon&s=25 MichaelLatta (Guest)
on 2007-06-17 15: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 Andren <rails-mailing-l...@andreas-s.net>
E6e0df436a7b752ccf182af73b05bc53?d=identicon&s=25 Lars Andren (pappa-gottis)
on 2007-06-20 05: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
4c3acdc3d93f54cc7a7281780ec8a4ee?d=identicon&s=25 shai (Guest)
on 2007-06-20 09: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
E6e0df436a7b752ccf182af73b05bc53?d=identicon&s=25 Lars Andren (pappa-gottis)
on 2007-06-23 07: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.