Hi! I''d like to move a record from one table to another. I''ve been reading the documentation, and I think I can accomplish this by finding the record, creating a new record with those attributes, and then removing the first one. First, is there a better way to do this? Next question, if I do go this way, do I have to specify every column that is going to be inserted into the database? For example, I would have to do this.... .new(:col1 => blah, :col2 => blah, :col3 => blah).save or can I just use the results from the previous find and insert them into the .new() part? I tried to put the @results in (.new(@results).save), but that obviously didn''t work. Thanks! --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
You could do it in one line: NewModel.create(OldModel.find(id).destroy.attributes) That provides no error checking or exception handling, of course. But I''m a sucker for oneliners. :-) Since it is destroying the old record before creating the new one, you may want to wrap it in a transaction. Or just expand it to more lines and ensure that the new record is created before destroying the old. However you prefer. The key is to use the attributes Hash. -matthew -- 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 -~----------~----~----~----~------~----~------~--~---
Ah, thank you very much... a little tweaking of the code and it worked. MainArchive.create(Response.find(params[:id]).destroy.attributes).save thanks for your help!!!! mike On Aug 13, 2:09 pm, Matthew Isleb <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> You could do it in one line: > > NewModel.create(OldModel.find(id).destroy.attributes) > > That provides no error checking or exception handling, of course. But > I''m a sucker for oneliners. :-) > > Since it is destroying the old record before creating the new one, you > may want to wrap it in a transaction. Or just expand it to more lines > and ensure that the new record is created before destroying the old. > However you prefer. The key is to use the attributes Hash. > > -matthew > -- > 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 -~----------~----~----~----~------~----~------~--~---
wiz561-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org wrote:> > MainArchive.create(Response.find(params[:id]).destroy.attributes).saveYou shouldn''t have to do that final save. create() saves for you. -- 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 -~----------~----~----~----~------~----~------~--~---