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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> 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 > > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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.savemeans 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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
> 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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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-- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---