mysql - 尝试更改迁移文件中数据库条目的更改大小写时出现“尝试调用私有(private)方法”错误

标签 mysql ruby-on-rails

class AddTitleToPosts < ActiveRecord::Migration
 def self.up
add_column :posts, :title, :string
    Post.find(:all).each do |post|
     post.update(:title => post.name.upcase)
    end
 end

 def self.down
 end

end

就像您可以做任何特别复杂的事情一样,只是尝试通过更改数据库中已有的名称列的大小写来添加新的列标题。但我尝试调用私有(private)方法时出错。我猜这与“ self ”有关?

感谢您的帮助。

最佳答案

由于您要添加列,因此您需要重置 ActiveRecord 有关模型的信息。 ActiveRecord::Migration 中的在更改表后使用模型部分展示了一个例子。这可能对你有用

class AddTitleToPosts < ActiveRecord::Migration
  def self.up
    add_column :posts, :title, :string

    Post.reset_column_information

    Post.find(:all).each do |post|
      post.update(:title => post.name.upcase)
    end
  end

  def self.down
  end
end

关于mysql - 尝试更改迁移文件中数据库条目的更改大小写时出现“尝试调用私有(private)方法”错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2703229/

相关文章:

mysql - 数据库json列在laravel Blade中返回转义字符串

php - 直接参数VS。在变量中保存值

mysql - MYSQL中将日期转换为日期范围---如何处理日期中的间隙

ruby-on-rails - AngularJS 和对 Oauth2 提供者的身份验证?

ruby-on-rails - Mac OSX 10.5 "good enough"上是否默认安装了用于 Rails 开发的 Ruby?

mysql - 每次在Mysql中运行查询时显示不同的数据

mysql - SQL 模式创建(头脑 Storm 有帮助吗?)

ruby-on-rails - 将文本字符串转换为任何有效的 Ruby 格式时间

ruby-on-rails - 分解具有相似模式的多条路线(Rails 4)

ruby-on-rails - 库未加载:/usr/local/opt/openssl/lib/libssl. 1.0.0.dylib(LoadError)