views:

60

answers:

2

I have got a bunch of records in locations table:

...
*************************** 8. row ***************************
        id: 8
      feed: http://feeds.bbc.co.uk/weather/feeds/rss/5day/world/4564.xml
...
*************************** 11. row ***************************
        id: 11
      feed: http://feeds.bbc.co.uk/weather/feeds/rss/5day/world/5007.xml
...

With the help of this migration:

  def self.up
    add_column :locations, :old_feed, :string
    Location.all.each do |l|
      l.old_feed = l.feed
      l.feed.sub!(/^.*?(\d+)\.xml$/, 'http://newsrss.bbc.co.uk/weather/forecast/\1/Next3DaysRSS.xml') # l.feed = l.feed.sub(...) does not make it any better
      l.save!
    end
  end

I want to turn them into

...
*************************** 8. row ***************************
        id: 8
      feed: http://newsrss.bbc.co.uk/weather/forecast/4564/Next3DaysRSS.xml
  old_feed: http://feeds.bbc.co.uk/weather/feeds/rss/5day/world/4564.xml
...
*************************** 11. row ***************************
        id: 11
      feed: http://newsrss.bbc.co.uk/weather/forecast/5007/Next3DaysRSS.xml
  old_feed: http://feeds.bbc.co.uk/weather/feeds/rss/5day/world/5007.xml
...

What I get instead is

...
*************************** 8. row ***************************
        id: 8
      feed: http://feeds.bbc.co.uk/weather/feeds/rss/5day/world/4564.xml
  old_feed: http://newsrss.bbc.co.uk/weather/forecast/4564/Next3DaysRSS.xml
...
*************************** 11. row ***************************
        id: 11
      feed: http://feeds.bbc.co.uk/weather/feeds/rss/5day/world/5007.xml
  old_feed: http://newsrss.bbc.co.uk/weather/forecast/5007/Next3DaysRSS.xml
...

Looks like feed and old_feed somehow got swapped and I can't figure out why.

Rails 2.3.4, MySQL 5.0.

P.S.
I bet it is something stupidly obvious I'm missing here.

+1  A: 

You are modifying the same object here:

l.old_feed = l.feed
l.feed.sub!(/^.*?(\d+)\.xml$/, 'http://newsrss.bbc.co.uk/weather/forecast/\1/Next3DaysRSS.xml') # l.feed = l.feed.sub(...) does not make it any better

it should be instead:

l.old_feed = l.feed.clone
l.feed.sub!(/^.*?(\d+)\.xml$/, 'http://newsrss.bbc.co.uk/weather/forecast/\1/Next3DaysRSS.xml') # l.feed = l.feed.sub(...) does not make it any better
KARASZI István
That doesn't seem to explain why feed retains old value.
artemave
Maybe some internals mixed up, but the problem lies there I think.
KARASZI István
You are right. molf answer explains why feed stayed the same.
artemave
+2  A: 
  1. You are modifying the same object (mentioned already).
  2. The feed attribute is not assigned anything new. You are modifying the return value in place.

These combined makes it look like you are reversing the strings. Funny, outcome actually. :-)

Try this instead:

def self.up
  add_column :locations, :old_feed, :string
  Location.all.each do |l|
    l.old_feed = l.feed
    # Reassign the new feed and drop the ! from sub!() to return a new string.
    l.feed = l.feed.sub(/^.*?(\d+)\.xml$/,
      'http://newsrss.bbc.co.uk/weather/forecast/\1/Next3DaysRSS.xml')
    l.save!
  end
end
molf