在 Rails 迁移中使用模型关联时出现问题

发布于 2024-11-02 05:41:03 字数 704 浏览 1 评论 0原文

我正在尝试解决一些问题,向评论表添加一列并执行一些更新。但一整天都出现这个错误:

耙子中止!发生错误, 所有后来的迁移都被取消:

当你没有的时候你有一个nil对象 期待吧!您可能期望 ActiveRecord::Base 的实例。这 评估 nil 时发生错误。[]

迁移代码:

class AddCommenterNameToComments < ActiveRecord::Migration
  def self.up
    add_column :comments, :commenter_name, :string
    Comment.reset_column_information

    #to update all comments with commenter name
    Comment.all.each do |comment|
      unless comment.is_system_message?
          comment.update_attribute(:commenter_name, comment.user.name )
      end
    end
  end

  def self.down
    remove_column :comments, :commenter_name
  end
end

请帮忙。

I´m trying to fix some problems adding a column to comments table and performing some updates. But all day having this error:

rake aborted! An error has occurred,
all later migrations canceled:

You have a nil object when you didn't
expect it! You might have expected an
instance of ActiveRecord::Base. The
error occurred while evaluating nil.[]

Migration code:

class AddCommenterNameToComments < ActiveRecord::Migration
  def self.up
    add_column :comments, :commenter_name, :string
    Comment.reset_column_information

    #to update all comments with commenter name
    Comment.all.each do |comment|
      unless comment.is_system_message?
          comment.update_attribute(:commenter_name, comment.user.name )
      end
    end
  end

  def self.down
    remove_column :comments, :commenter_name
  end
end

Please, help.

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

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

发布评论

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

评论(1

深海蓝天 2024-11-09 05:41:03

关联可能不是问题,但要测试是否是问题,您可以通过传入 Comments 表中的 user_id 列来搜索 User 对象。因此,例如:
使用 OP 进行编辑 建议的修复

class AddCommenterNameToComments < ActiveRecord::Migration
  class Comment < ActiveRecord::Base
  end
  class User < ActiveRecord::Base
  end
  def self.up
    add_column :comments, :commenter_name, :string
    Comment.reset_column_information

    #to update all comments with commenter name
    Comment.all.each do |comment|
      unless comment.is_system_message?
        user = User.find_by_id(comment.user_id)
        if user
          comment.update_attribute(:commenter_name, user.name )
        else
          comment.update_attribute(:commenter_name, "User deleted" )
        end
      end
    end
  end

  def self.down
    remove_column :comments, :commenter_name
  end
end

现在也就是说,问题可能是您在 comments 表中的 user_id 不再对应于 Users 表中的 User 对象。您可能还想检查一下,以防关联不是问题所在。

The association might not be the problem, but to test if it is you could search the User object by passing in the user_id column from the Comments table. So, for example:
Edited with OP Proposed fix

class AddCommenterNameToComments < ActiveRecord::Migration
  class Comment < ActiveRecord::Base
  end
  class User < ActiveRecord::Base
  end
  def self.up
    add_column :comments, :commenter_name, :string
    Comment.reset_column_information

    #to update all comments with commenter name
    Comment.all.each do |comment|
      unless comment.is_system_message?
        user = User.find_by_id(comment.user_id)
        if user
          comment.update_attribute(:commenter_name, user.name )
        else
          comment.update_attribute(:commenter_name, "User deleted" )
        end
      end
    end
  end

  def self.down
    remove_column :comments, :commenter_name
  end
end

Now that said, the problem could be that you have a user_id in the comments table which no longer corresponds to a User object in the Users table. You might want to check for that as well, in case the association turns out not to be the problem.

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