Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to update all the values of an column with regular expression in Rails 4

I have a table with an column "Ip" all has the values like 10.x.x.x, now I want to replace the 10 with 20 of all the records. Is their any way to do this via Activerecord or Do I need to execute only raw postgres query with regexp_replace?

like image 589
loganathan Avatar asked Mar 13 '14 11:03

loganathan


1 Answers

ModelName.update_all("column_name = replace(columnname,'originaltext','replacement') ")

Eg:

Resource.update_all("fqdn = replace(fqdn,'d3p5','d6p6') ")

Now all the occurrences of "d3p5" of fqdn column replaced with d6p6

like image 111
loganathan Avatar answered Nov 01 '22 14:11

loganathan