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

1

6

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?

Franklyn answered 13/3, 2014 at 11:34 Comment(1)
Both are possible. You could just fire up the rails console and execute some code to change the ip addresses. Test in development mode first, of course.Paulinapauline
F
6
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

Franklyn answered 13/3, 2014 at 11:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.