尝试使用 gsub 更改属性时出现迁移问题
我有一个名为 items 的表。我想更改 9 个对象的描述属性(它是文本属性)。具体来说,这些对象是 Item 的子类——称为 Juice。所以项目表是 STI。以下是该项目的示例描述:
将所有付款提高 20%。
现在,当我尝试运行以下迁移时,我无法更新该死的描述。有什么想法吗? (Rails 版本为 2.3.11。)
class ModifyItemJuiceDescription < ActiveRecord::Migration
def self.up
juices = Juice.all
Juice.transaction do
for j in juices do
puts "Juice description is: #{j.description}."
j.description.gsub!('payouts', 'winnings')
puts "Juice description will be saved as: #{j.description}."
j.save!
puts "Juice description is now: #{j.description}."
puts "======================================================"
end
end
end
def self.down
juices = Juice.all
Juice.transaction do
for j in juices do
puts "Juice description is: #{j.description}."
j.description.gsub!('winnings', 'payouts')
puts "Juice description will be saved as: #{j.description}."
j.save!
puts "Juice description is now: #{j.description}."
puts "======================================================"
end
end
end
end
I have a table called items. I want to change the description attribute (it's a text attribute) for 9 of the objects. Specifically, these objects are a subclass to Item -- called Juice. So the items table is STI. Here's a sample description of the item:
Boost all payouts by 20%.
Now, when I try to run the following migration, I can't get the dang description to update. Any ideas? (Rails version is 2.3.11.)
class ModifyItemJuiceDescription < ActiveRecord::Migration
def self.up
juices = Juice.all
Juice.transaction do
for j in juices do
puts "Juice description is: #{j.description}."
j.description.gsub!('payouts', 'winnings')
puts "Juice description will be saved as: #{j.description}."
j.save!
puts "Juice description is now: #{j.description}."
puts "======================================================"
end
end
end
def self.down
juices = Juice.all
Juice.transaction do
for j in juices do
puts "Juice description is: #{j.description}."
j.description.gsub!('winnings', 'payouts')
puts "Juice description will be saved as: #{j.description}."
j.save!
puts "Juice description is now: #{j.description}."
puts "======================================================"
end
end
end
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我有一种预感,
j.description
返回字符串的副本,而不是映射到数据库的实际结构;格苏!调用更改了错误的对象。尝试
j.description = j.description.gsub(...)
I have a hunch that
j.description
returns a copy of the string, not the actual structure that maps to the database; The gsub! call changes the wrong object.Try
j.description = j.description.gsub(...)