0

I have a Rails 3.2 app where I would like to copy the contents of one field to another in a controller action.

Costprojects contains these 2 columns:

original_year
project_year

I want to copy the contents of project_year into original_year.

I tried doing this:

    update = Costproject.where(:id => @costprojects).update_all(:original_year => :project_year)

I get this error:

PG::Error: ERROR:  invalid input syntax for integer: "project_year"

LINE 1: UPDATE "costprojects" SET "original_year" = 'project_year' W...

                                                ^

Thanks for the help!

Reddirt
  • 5,913
  • 9
  • 48
  • 126
  • Are both columns have same datatype ? plz check – Arup Rakshit Jun 02 '15 at 19:34
  • I am sure not.. use [this](http://stackoverflow.com/questions/22060851/rails-migration-pgerror-error-invalid-input-syntax-for-integer) or [this](http://stackoverflow.com/questions/20715190/pginvalidtextrepresentation-error-invalid-input-syntax-for-integer-m) as a solution. – Arup Rakshit Jun 02 '15 at 19:42
  • Both columns are integer. – Reddirt Jun 02 '15 at 19:49

1 Answers1

0

Please try this :

Costproject.where(:id => @costprojects)
           .update_all("original_year = project_year")

With your former code:

update_all(:original_year => :project_year)

It was trying to update an integer field original_year with a string value project_year. That's why you got the error.

Arup Rakshit
  • 116,827
  • 30
  • 260
  • 317