听起来像 ActiveRecord 在保存时弄乱了对象属性

发布于 2024-08-17 09:56:42 字数 1886 浏览 2 评论 0原文

我在 locations 表中有一堆记录:

...
*************************** 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
...

在这次迁移的帮助下:

  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

我想将它们变成

...
*************************** 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
...

我得到的,

...
*************************** 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
...

看起来像 feedold_feed 不知何故被交换了,我不明白为什么。

Rails 2.3.4、MySQL 5.0。

附注
我敢打赌这是我在这里错过的一些愚蠢而明显的东西。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

爱你是孤单的心事 2024-08-24 09:56:42
  1. 您正在修改同一个对象(已经提到过)。
  2. feed 属性未分配任何新内容。您正在就地修改返回值。

这些组合起来看起来就像你正在颠倒琴弦。有趣的是,结果实际上。 :-)

试试这个:

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
  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
歌入人心 2024-08-24 09:56:42

您正在此处修改同一对象:

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.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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文