Question: I am running the following code on a model containing website urls. It should strip off the 'www.' from every url and save the record back to the db. However, there are records where the changed url simply will not be saved. I have tested the .sub routine via the console and it does make the changes to the string, however, the change isn't saved.

```
def strip
   b = Sites.all
   b.each do |t|
     t.url.sub!(/www./, '') 
     t.save
  end
end

```

I also ran the above code directly in the rails console and the output was as follows (again, nothing was saved):

```
   (0.0ms)  commit transaction
   (0.0ms)  begin transaction
   (0.0ms)  commit transaction
   (0.0ms)  begin transaction
   (0.0ms)  commit transaction
   (0.0ms)  begin transaction
   (0.0ms)  commit transaction
   (0.0ms)  begin transaction
   (0.0ms)  commit transaction

```

Answer: 
Don't change activerecord model attributes in place - you'll confuse the change tracking and activerecord will think you've made no changes. 

Instead do

```
t.url = t.url.sub(/www\./,'')

```

You should also escape the `.` in the URL or you'd match any character after the www.
Try changing:

```
    t.url.sub!(/www./, '')

```

to

```
    t.url= t.url.sub(/www./, '')

```

Activerecord tracks changes and only fields that, it thinks, are changed are part of the update clause. Directly changing t.url using sub! is not marking the field as changed (maybe a bug in Activerecord).

Another suggestion change your pattern to /www\./, otherwise . matches with any character.