Hi All, I need to update the records in the mysql with the data fields from the xml where the id field not presented in the database, instead we took the number as primary key but not auto_incremented. I treid with many attempts for example my view as follows .. <h1>Editing employee</h1> <% form_tag :action =>''update'',:id => @employee.number do -%> <table> <tr> <td> <%= label ''Employ_No'',''Employee Number'' %> </td> <td> <%= text_field ''employee'',''number'',:value => @employee.number,:size => "16",:maxlength => "20",:id=>''number'' %> </td> </tr> <tr> <td> <%= label ''Employ_Name'',''Employee Name'' %> </td> <td> <%= text_field ''employee'',''name'',:value => @employee.name,:size => "16",:maxlength => "20",:id=>"employ_name" %> </td> </tr> <tr> <td> <%= label ''Employ_Job'',''Employee Job'' %> </td> <td> <%= text_field ''employee'',''job'',:value => @employee.job,:size => "16",:maxlength => "20",:id=>"employ_job" %> </td> </tr> <tr> <td> <%= label ''Employ_Salary'',''Employee Salary'' %> </td> <td> <%= text_field ''employee'',''salary'',:value => @employee.salary,:size => "16",:maxlength => "20",:id=>"employ_salary" %> </td> </tr> </table> <%= submit_tag "Save" %> <% end %> I tried with text_field_tag but i found this is the right way since we can access through the params[:employee] for all values and params[:employee][:name] for individual fields and in my controller def update begin puts "In update" no = params[:id] employ = Employee.new employ.number = params[:employee][:number] employ.name = params[:employee][:name] employ.job = params[:employee][:job] employ.salary = params[:employee][:salary] # puts "#{params[employ_no]}" puts "#{params[:employee][:employ_no]}" puts "#{params[:employee][:name]}" puts "#{params[:id]}" employee = Employee.find(:first,:conditions => ["number = ?",no]) @emp = Employee.find(:first,:conditions => ["number = ?",no]) puts "before update" # if @emp.update(:no,params[:employee]) then # puts "success" # reder :text => "success" # else # puts "failed" # render :text => "failed" # end if @emp.update_attributes(params[:employee]) then puts "success" reder :text => "success" else puts "failed" render :text => "failed" end # employee.update_attribute(:number,employ.number) puts "updated" #if @emp!= nil then # @emp.number = params[:employ_no] # @emp.name = params[:employ_name] # @emp.job = params[:employ_job] # @emp.salary = params[:employ_sal] # end puts "#{params[:employee]}" puts @emp rescue => e puts "error is #{e}" end end Where my model has only 4 fields name,number,job,salary (here number is the primary key) I tried with all the cases and vexed, It is througin error for each case @emp.update(:number,params[:employee]) # ERROR : The private method ''update'' called @emp.update_attributes(params[:employee]) # Mysql::Error: #42S22Unknown column ''id'' in ''where clause'': UPDATE `employees` SET `salary` = ''20000'', `number` = 6, `job` = ''se'', `name` = ''xxx'' WHERE `id` = NULL @emp.update_attributes(employ) # ERROR is undefined method `stringify_keys!'' for #<Employee> How to achieve the updating? Please anybody help me.. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
On 19 Nov 2008, at 11:52, hema gonaboina wrote:> Hi All, > I need to update the records in the mysql with the data > fields from the xml where the id field not presented in the database, > instead we took the number as primary key but not auto_incremented. > > I treid with many attempts > for example my view as follows .. >Your code is pretty impenetrable by virtue of the duplication, bits commented out etc... but if you don''t have a primary key column you should be using set_primary_key to tell rails which column is the primary key Fred> <h1>Editing employee</h1> > <% form_tag :action =>''update'',:id => @employee.number do -%> > <table> > <tr> > <td> > <%= label ''Employ_No'',''Employee Number'' %> > </td> > <td> > <%= text_field ''employee'',''number'',:value => > @employee.number,:size => "16",:maxlength => "20",:id=>''number'' %> > > </td> > </tr> > <tr> > <td> > <%= label ''Employ_Name'',''Employee Name'' %> > </td> > <td> > <%= text_field ''employee'',''name'',:value => > @employee.name,:size => "16",:maxlength => "20",:id=>"employ_name" %> > </td> > </tr> > <tr> > <td> > <%= label ''Employ_Job'',''Employee Job'' %> > </td> > <td> > <%= text_field ''employee'',''job'',:value => > @employee.job,:size => "16",:maxlength => "20",:id=>"employ_job" %> > </td> > </tr> > <tr> > <td> > <%= label ''Employ_Salary'',''Employee Salary'' %> > </td> > <td> > <%= text_field ''employee'',''salary'',:value => > @employee.salary,:size => "16",:maxlength => > "20",:id=>"employ_salary" %> > </td> > </tr> > </table> > <%= submit_tag "Save" %> > <% end %> > > > I tried with text_field_tag but i found this is the right way since > we can access through the params[:employee] for all values and > params[:employee][:name] for individual fields > > and in my controller > > def update > begin > puts "In update" > no = params[:id] > employ = Employee.new > employ.number = params[:employee][:number] > employ.name = params[:employee][:name] > employ.job = params[:employee][:job] > employ.salary = params[:employee][:salary] > # puts "#{params[employ_no]}" > puts "#{params[:employee][:employ_no]}" > puts "#{params[:employee][:name]}" > puts "#{params[:id]}" > > employee = Employee.find(:first,:conditions => ["number > = ?",no]) > > @emp = Employee.find(:first,:conditions => ["number > = ?",no]) > > puts "before update" > > # if @emp.update(:no,params[:employee]) then > # puts "success" > # reder :text => "success" > # else > # puts "failed" > # render :text => "failed" > # end > > if @emp.update_attributes(params[:employee]) then > puts "success" > reder :text => "success" > else > puts "failed" > render :text => "failed" > end > > # employee.update_attribute(:number,employ.number) > > puts "updated" > #if @emp!= nil then > # @emp.number = params[:employ_no] > # @emp.name = params[:employ_name] > # @emp.job = params[:employ_job] > # @emp.salary = params[:employ_sal] > # end > puts "#{params[:employee]}" > puts @emp > > > > rescue => e > puts "error is #{e}" > end > end > > > Where my model has only 4 fields name,number,job,salary (here number > is the primary key) > I tried with all the cases and vexed, It is througin error for each > case > > @emp.update(:number,params[:employee]) # ERROR : The private method > ''update'' called > > @emp.update_attributes(params[:employee]) # Mysql::Error: > #42S22Unknown column ''id'' in ''where clause'': UPDATE `employees` SET > `salary` = ''20000'', `number` = 6, `job` = ''se'', `name` = ''xxx'' WHERE > `id` = NULL > > @emp.update_attributes(employ) # ERROR is undefined method > `stringify_keys!'' for #<Employee> > > > How to achieve the updating? Please anybody help me.. > > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Thank You Fred for your reply.. I tried with the set_primary_key but still it is throwing the exception,Sorry sending the code like that i modified the code with removing the comments,the commented code is the my trails to achieve the desired.I have given the ways and the errors that are rendering for the particular call .. Please check and excuse the mistakes if any,> > Hi All, > > I need to update the records in the mysql with the data > > fields from the xml where the id field not presented in the database, > > instead we took the number as primary key but not auto_incremented. > > > I treid with many attempts > > for example my view as follows .. > > Your code is pretty impenetrable by virtue of the duplication, bits > commented out etc... but if you don''t have a primary key column you > should be using set_primary_key to tell rails which column is the > primary key > > Fred > > > <h1>Editing employee</h1> > > <% form_tag :action =>''update'',:id => @employee.number do -%> > > <table> > > <tr> > > <td> > > <%= label ''Employ_No'',''Employee Number'' %> > > </td> > > <td> > > <%= text_field ''employee'',''number'',:value => > > @employee.number,:size => "16",:maxlength => "20",:id=>''number'' %> > > > </td> > > </tr> > > <tr> > > <td> > > <%= label ''Employ_Name'',''Employee Name'' %> > > </td> > > <td> > > <%= text_field ''employee'',''name'',:value => > > @employee.name,:size => "16",:maxlength => "20",:id=>"employ_name" %> > > </td> > > </tr> > > <tr> > > <td> > > <%= label ''Employ_Job'',''Employee Job'' %> > > </td> > > <td> > > <%= text_field ''employee'',''job'',:value => > > @employee.job,:size => "16",:maxlength => "20",:id=>"employ_job" %> > > </td> > > </tr> > > <tr> > > <td> > > <%= label ''Employ_Salary'',''Employee Salary'' %> > > </td> > > <td> > > <%= text_field ''employee'',''salary'',:value => > > @employee.salary,:size => "16",:maxlength => > > "20",:id=>"employ_salary" %> > > </td> > > </tr> > > </table> > > <%= submit_tag "Save" %> > > <% end %> > > > I tried with text_field_tag but i found this is the right way since > > we can access through the params[:employee] for all values and > > params[:employee][:name] for individual fields > > > and in my controller > > > def update > > begin > > puts "In update" > > no = params[:id] > > employ = Employee.new > > employ.number = params[:employee][:number] > > employ.name = params[:employee][:name] > > employ.job = params[:employee][:job] > > employ.salary = params[:employee][:salary] > > > > > employee = Employee.find(:first,:conditions => ["number = ?",no]) > > > @emp = Employee.find(:first,:conditions => ["number = ?",no]) > > > if @emp.update_attributes(params[:employee]) then > > puts "success" > > reder :text => "success" > > else > > puts "failed" > > render :text => "failed" > > end > > > rescue => e > > puts "error is #{e}" > > end > > end > > > Where my model has only 4 fields name,number,job,salary (here number > > is the primary key) > > I tried with all the cases and vexed, It is throwing error for each case as I tried are > > > @emp.update(:number,params[:employee]) for this the error i am getting as # ERROR : The private method ''update'' called > > > @emp.update_attributes(params[:employee]) for this the error is # Mysql::Error: > > #42S22Unknown column ''id'' in ''where clause'': UPDATE `employees` SET > > `salary` = ''20000'', `number` = 6, `job` = ''se'', `name` = ''xxx'' WHERE > > `id` = NULL > > > @emp.update_attributes(employ) for this i am getting the error as # ERROR is undefined method > > `stringify_keys!'' for #<Employee> > > > How to achieve the updating? Please anybody help me..--~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Thank You Fred I got it In model I kept the statement as self.primary_key = :number It worked.. Thank you On Nov 19, 4:59 pm, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 19 Nov 2008, at 11:52, hema gonaboina wrote: > > > Hi All, > > I need to update the records in the mysql with the data > > fields from the xml where the id field not presented in the database, > > instead we took the number as primary key but not auto_incremented. > > > I treid with many attempts > > for example my view as follows .. > > Your code is pretty impenetrable by virtue of the duplication, bits > commented out etc... but if you don''t have a primary key column you > should be using set_primary_key to tell rails which column is the > primary key > > Fred > > > <h1>Editing employee</h1> > > <% form_tag :action =>''update'',:id => @employee.number do -%> > > <table> > > <tr> > > <td> > > <%= label ''Employ_No'',''Employee Number'' %> > > </td> > > <td> > > <%= text_field ''employee'',''number'',:value => > > @employee.number,:size => "16",:maxlength => "20",:id=>''number'' %> > > > </td> > > </tr> > > <tr> > > <td> > > <%= label ''Employ_Name'',''Employee Name'' %> > > </td> > > <td> > > <%= text_field ''employee'',''name'',:value => > > @employee.name,:size => "16",:maxlength => "20",:id=>"employ_name" %> > > </td> > > </tr> > > <tr> > > <td> > > <%= label ''Employ_Job'',''Employee Job'' %> > > </td> > > <td> > > <%= text_field ''employee'',''job'',:value => > > @employee.job,:size => "16",:maxlength => "20",:id=>"employ_job" %> > > </td> > > </tr> > > <tr> > > <td> > > <%= label ''Employ_Salary'',''Employee Salary'' %> > > </td> > > <td> > > <%= text_field ''employee'',''salary'',:value => > > @employee.salary,:size => "16",:maxlength => > > "20",:id=>"employ_salary" %> > > </td> > > </tr> > > </table> > > <%= submit_tag "Save" %> > > <% end %> > > > I tried with text_field_tag but i found this is the right way since > > we can access through the params[:employee] for all values and > > params[:employee][:name] for individual fields > > > and in my controller > > > def update > > begin > > puts "In update" > > no = params[:id] > > employ = Employee.new > > employ.number = params[:employee][:number] > > employ.name = params[:employee][:name] > > employ.job = params[:employee][:job] > > employ.salary = params[:employee][:salary] > > # puts "#{params[employ_no]}" > > puts "#{params[:employee][:employ_no]}" > > puts "#{params[:employee][:name]}" > > puts "#{params[:id]}" > > > employee = Employee.find(:first,:conditions => ["number > > = ?",no]) > > > @emp = Employee.find(:first,:conditions => ["number > > = ?",no]) > > > puts "before update" > > > # if @emp.update(:no,params[:employee]) then > > # puts "success" > > # reder :text => "success" > > # else > > # puts "failed" > > # render :text => "failed" > > # end > > > if @emp.update_attributes(params[:employee]) then > > puts "success" > > reder :text => "success" > > else > > puts "failed" > > render :text => "failed" > > end > > > # employee.update_attribute(:number,employ.number) > > > puts "updated" > > #if @emp!= nil then > > # -r7ef+o6UWKmpuJRAN1am9g@public.gmane.org = params[:employ_no] > > # @emp.name = params[:employ_name] > > # @emp.job = params[:employ_job] > > # @emp.salary = params[:employ_sal] > > # end > > puts "#{params[:employee]}" > > puts @emp > > > rescue => e > > puts "error is #{e}" > > end > > end > > > Where my model has only 4 fields name,number,job,salary (here number > > is the primary key) > > I tried with all the cases and vexed, It is througin error for each > > case > > > @emp.update(:number,params[:employee]) # ERROR : The private method > > ''update'' called > > > @emp.update_attributes(params[:employee]) # Mysql::Error: > > #42S22Unknown column ''id'' in ''where clause'': UPDATE `employees` SET > > `salary` = ''20000'', `number` = 6, `job` = ''se'', `name` = ''xxx'' WHERE > > `id` = NULL > > > @emp.update_attributes(employ) # ERROR is undefined method > > `stringify_keys!'' for #<Employee> > > > How to achieve the updating? Please anybody help me..--~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---