Update_attributes, want to add not replace?


#1

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


#2

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.

:slight_smile:

hth

shai


#3

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


#4

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 :slight_smile:

enjoy


#5

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 :slight_smile:

enjoy


#6

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