Hi, This has really thrown me as I work up a first demo. app. My record comes back from a web form with it''s correct key id=21 (thats after the first pp ) After the update a pp dump shows it''s been reset to the next key available. Whats happening here please, and how do I get "my" key to be used, not an auto-generated one. I want to do an update of the record, not an insert. Cheers, Bob ---------------------------------------------------------------------------------------------------------- <snip> def save pp params record=Record.create(params[:record]); pp record record.save() end <snip> ------------------------------- pp first ---------------------------------- {"commit"=>"update", "action"=>"save", "id"=>"21", "controller"=>"editor", "record"=> {"title"=>"Advanced biochemistry online course material", "url"=>"http://info.bio.cmu.edu/courses/03740/ABC98.html", "id"=>"21", "description"=> "A series of resources designed to support the Advanced Biochemistry \r\ncourse at Carnegie Mellon University. The Web part of the course is \r\nbased around twelve problem sets and the materials required to examine these problems. Topics include protein and nucleic acid structures, protein folding, activation of Ras GTPase by GAPs, selective inhibitors of isozyme 2 of cyclooxygenase, DNA & RNA structures, combinatorial approaches to collagenase inhibitors, ribosome structure & mRNA decoding, catalysis of peptide bond formation by rRNA, regulation of MAP kinase in development, cooperativity of T7 DNA helicase, FtsZ as a prokaryotic tubulin, and SMC proteins and antiparallel coiled-coil interactions. Resources available include tutorials, graphics, and and tools."}} --------------------- pp second ----------------------- #<Record:0x8efa03c @attributes {"title"=>"Advanced biochemistry online course material", "url"=>"http://info.bio.cmu.edu/courses/03740/ABC98.html", "id"=>28, "description"=> "A series of resources designed to support the Advanced Biochemistry \r\ncourse at Carnegie Mellon University. The Web part of the course is \r\nbased around twelve problem sets and the materials required to examine these problems. Topics include protein and nucleic acid structures, protein folding, activation of Ras GTPase by GAPs, selective inhibitors of isozyme 2 of cyclooxygenase, DNA & RNA structures, combinatorial approaches to collagenase inhibitors, ribosome structure & mRNA decoding, catalysis of peptide bond formation by rRNA, regulation of MAP kinase in development, cooperativity of T7 DNA helicase, FtsZ as a prokaryotic tubulin, and SMC proteins and antiparallel coiled-coil interactions. Resources available include tutorials, graphics, and and tools."}, @errors #<ActiveRecord::Errors:0x8ef4b64 @base=#<Record:0x8efa03c ...>, @errors={}>, @new_record=false> This message has been checked for viruses but the contents of an attachment may still contain software viruses, which could damage your computer system: you are advised to perform your own checks. Email communications with the University of Nottingham may be monitored as permitted by UK legislation.
Its because you are "creating" a new record every time you call save. What you want is something more like: #this is for an existing record record = Record.find(params[:id]) record.update_attributes(params[:record]) #this will update/save the record What you have will work fine for a brand new record, but you could remove the record.save as create does it all in this case. -Nick On 10/25/05, Bob Parkinson <rwp-IKR479FllOH10XsdtD+oqA@public.gmane.org> wrote:> Hi, > > This has really thrown me as I work up a first demo. app. > > My record comes back from a web form with it''s correct key id=21 (thats after the first pp ) > > After the update a pp dump shows it''s been reset to the next key available. Whats happening here please, and how do I get "my" key to be used, not an auto-generated one. > > I want to do an update of the record, not an insert. > > Cheers, > > Bob > > ---------------------------------------------------------------------------------------------------------- > > <snip> > > def save > > pp params > > record=Record.create(params[:record]); > > pp record > > record.save() > > end > > <snip> > > ------------------------------- > pp first > ---------------------------------- > {"commit"=>"update", > "action"=>"save", > "id"=>"21", > "controller"=>"editor", > "record"=> > {"title"=>"Advanced biochemistry online course material", > "url"=>"http://info.bio.cmu.edu/courses/03740/ABC98.html", > "id"=>"21", > "description"=> > "A series of resources designed to support the Advanced Biochemistry \r\ncourse at Carnegie Mellon University. The Web part of the course is \r\nbased around twelve problem sets and the materials required to examine these problems. Topics include protein and nucleic acid structures, protein folding, activation of Ras GTPase by GAPs, selective inhibitors of isozyme 2 of cyclooxygenase, DNA & RNA structures, combinatorial approaches to collagenase inhibitors, ribosome structure & mRNA decoding, catalysis of peptide bond formation by rRNA, regulation of MAP kinase in development, cooperativity of T7 DNA helicase, FtsZ as a prokaryotic tubulin, and SMC proteins and antiparallel coiled-coil interactions. Resources available include tutorials, graphics, and and tools."}} > --------------------- > pp second > ----------------------- > > #<Record:0x8efa03c > @attributes> {"title"=>"Advanced biochemistry online course material", > "url"=>"http://info.bio.cmu.edu/courses/03740/ABC98.html", > "id"=>28, > "description"=> > "A series of resources designed to support the Advanced Biochemistry \r\ncourse at Carnegie Mellon University. The Web part of the course is \r\nbased around twelve problem sets and the materials required to examine these problems. Topics include protein and nucleic acid structures, protein folding, activation of Ras GTPase by GAPs, selective inhibitors of isozyme 2 of cyclooxygenase, DNA & RNA structures, combinatorial approaches to collagenase inhibitors, ribosome structure & mRNA decoding, catalysis of peptide bond formation by rRNA, regulation of MAP kinase in development, cooperativity of T7 DNA helicase, FtsZ as a prokaryotic tubulin, and SMC proteins and antiparallel coiled-coil interactions. Resources available include tutorials, graphics, and and tools."}, > @errors> #<ActiveRecord::Errors:0x8ef4b64 @base=#<Record:0x8efa03c ...>, @errors={}>, > @new_record=false> > > > > This message has been checked for viruses but the contents of an attachment > may still contain software viruses, which could damage your computer system: > you are advised to perform your own checks. Email communications with the > University of Nottingham may be monitored as permitted by UK legislation. > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
record = Record.create(params[:record]) is creating a new record object, saving it to the database (assuming it passes validation) and returns the new record. so what i am seeing makes perfect sense. you are using the attributes from an existing record to create a brand new record. the record.save() is not needed as create has already saved the record. to update an existing record, just do something like: RecordController < ActiveController::Base ... def update @record = Record.find(@params[:id]) if record.update_attributes(@params[:record]) flash[:notice] = ''record updated successfully'' redirect_to :action => :show, :id = @record else render_action :edit end end On 10/25/05, Bob Parkinson <rwp-IKR479FllOH10XsdtD+oqA@public.gmane.org> wrote:> > Hi, > > This has really thrown me as I work up a first demo. app. > > My record comes back from a web form with it''s correct key id=21 (thats > after the first pp ) > > After the update a pp dump shows it''s been reset to the next key > available. Whats happening here please, and how do I get "my" key to be > used, not an auto-generated one. > > I want to do an update of the record, not an insert. > > Cheers, > > Bob > > > ---------------------------------------------------------------------------------------------------------- > > <snip> > > def save > > pp params > > record=Record.create(params[:record]); > > pp record > > record.save() > > end > > <snip> > > ------------------------------- > pp first > ---------------------------------- > {"commit"=>"update", > "action"=>"save", > "id"=>"21", > "controller"=>"editor", > "record"=> > {"title"=>"Advanced biochemistry online course material", > "url"=>"http://info.bio.cmu.edu/courses/03740/ABC98.html", > "id"=>"21", > "description"=> > "A series of resources designed to support the Advanced Biochemistry > \r\ncourse at Carnegie Mellon University. The Web part of the course is > \r\nbased around twelve problem sets and the materials required to examine > these problems. Topics include protein and nucleic acid structures, protein > folding, activation of Ras GTPase by GAPs, selective inhibitors of isozyme 2 > of cyclooxygenase, DNA & RNA structures, combinatorial approaches to > collagenase inhibitors, ribosome structure & mRNA decoding, catalysis of > peptide bond formation by rRNA, regulation of MAP kinase in development, > cooperativity of T7 DNA helicase, FtsZ as a prokaryotic tubulin, and SMC > proteins and antiparallel coiled-coil interactions. Resources available > include tutorials, graphics, and and tools."}} > --------------------- > pp second > ----------------------- > > #<Record:0x8efa03c > @attributes> {"title"=>"Advanced biochemistry online course material", > "url"=>"http://info.bio.cmu.edu/courses/03740/ABC98.html", > "id"=>28, > "description"=> > "A series of resources designed to support the Advanced Biochemistry > \r\ncourse at Carnegie Mellon University. The Web part of the course is > \r\nbased around twelve problem sets and the materials required to examine > these problems. Topics include protein and nucleic acid structures, protein > folding, activation of Ras GTPase by GAPs, selective inhibitors of isozyme 2 > of cyclooxygenase, DNA & RNA structures, combinatorial approaches to > collagenase inhibitors, ribosome structure & mRNA decoding, catalysis of > peptide bond formation by rRNA, regulation of MAP kinase in development, > cooperativity of T7 DNA helicase, FtsZ as a prokaryotic tubulin, and SMC > proteins and antiparallel coiled-coil interactions. Resources available > include tutorials, graphics, and and tools."}, > @errors> #<ActiveRecord::Errors:0x8ef4b64 @base=#<Record:0x8efa03c ...>, > @errors={}>, > @new_record=false> > > > > This message has been checked for viruses but the contents of an > attachment > may still contain software viruses, which could damage your computer > system: > you are advised to perform your own checks. Email communications with the > University of Nottingham may be monitored as permitted by UK legislation. > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >_______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
2005/10/25, Bob Parkinson <rwp@biome.ac.uk>:> I want to do an update of the record, not an insert.Fetch the record from the DB, and then update the values. If you don't fetch from the DB, ActiveRecord won't know that you're trying to update. Use this: model = Model.find(params[:id]) if model.update_attributes(params[:model]) then # save success else # save failed end Hope that helps ! François _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Thank you for yor reply, that has been very helpful. I''m working with the book ''agile web development...'' and I''d seen the update example, but had not taken in some of the way it works in rails. Best wishes, Bob This message has been checked for viruses but the contents of an attachment may still contain software viruses, which could damage your computer system: you are advised to perform your own checks. Email communications with the University of Nottingham may be monitored as permitted by UK legislation.