ClassyPimp
ClassyPimp

Reputation: 725

Rails and SQL appending string to database field

We have a table:

id____|_____name
1     |    john
2     |    jane

What is the best way to append some string to name column, resulting in

     id___|___name
     1    |   john doe
     2    |   jane doe

Extra question: When using sql CONCAT for that purposes does it require the same computational cost as for retrieving data and than saving it (table.find(1)->.name = name + "doe"->.save), or (how does CONCAT work? Does it first read field and than uses UPDATE or appending it without retrieving?)?

edit

i've found increment_counter method wich makes atomic increment, well is there any similar method for atomic string appending, I mean the one that will append it in one transaction. Or what is the best way to update log field?

Upvotes: 0

Views: 1036

Answers (1)

Arup Rakshit
Arup Rakshit

Reputation: 118261

In postgres

Model.update_all("name = name || doe")

In Mysql

Model.update_all("name = concat(name, 'doe')")

Upvotes: 1

Related Questions