【发布时间】:2015-10-19 06:56:44
【问题描述】:
我使用迁移将两个字段添加到名为 brands 的现有表中:
def change
add_column :brands, :about_the_brand, :string, limit: 2000
add_column :brands, :user_id, :integer
end
迁移运行良好。在 Rails 控制台中:
brand = Brand.first
brand.user_id = 2
brand.save
我明白了:
(0.2ms) BEGIN
(0.1ms) ROLLBACK
TypeError: nil is not a symbol
我在任何地方都找不到对此错误的任何有意义的参考。这是一个非常简单的操作,我看到它确实没有失败的理由。
错误来自:
activemodel-4.2.3/lib/active_model/dirty.rb:181:in `attribute_was'
这是:
# Handle <tt>*_was</tt> for +method_missing+.
def attribute_was(attr) # :nodoc:
attribute_changed?(attr) ? changed_attributes[attr] : __send__(attr)
end
【问题讨论】:
-
您能检查一下您的品牌表中是否有 id 列吗?当您尝试使用非默认主键创建 activerecord 模型时,可能会出现此消息
-
很棒的收获。我在这张表中没有 id。它是从外部源填充的,并且永远无法插入。我需要做的就是更新它。但我显然不能在没有声明唯一 ID 的情况下使用 AR。
标签: ruby-on-rails ruby-on-rails-4 ruby-on-rails-4.2