警告:无法批量分配这些受保护的属性:id(ar-extensions)
我正在使用 Rails 2.3.5 和 AR-extensions 0.9.3
我正在尝试从一个表批量插入到位于不同服务器/数据库上的另一个表。但我不想覆盖任何内容。只需在新表末尾进行简单的插入就足够了。
我注意到我收到此警告消息: 警告:无法批量分配这些受保护的属性:id
我以前的条目正在被覆盖..那么我该如何解决这个问题?
谢谢!
编辑:弄清楚了。看起来我所要做的就是定义一个我想要的属性数组(不包括 id)并将其输入导入函数中。
更新:
tableA_items = TableA.find(:all)
TableB.establish_connection("other_server")
TableB.import tableA_items
I'm using Rails 2.3.5 and AR-extensions 0.9.3
I'm trying to bulk insert from one table to another table located on a different server / database. I don't want anything overwritten though. Just a simple insert at the end of the new table is good enough.
I noticed that I get this warning message:
WARNING: Can't mass-assign these protected attributes: id
My former entries are being overwritten.. so how do I work around this?
Thanks!
Edit: Figured it out. Looks like all I have to is define an array of attributes I want (Excluding id) and feed that into the import function.
Update:
tableA_items = TableA.find(:all)
TableB.establish_connection("other_server")
TableB.import tableA_items
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
此错误存在于 ar-extensions(直至 0.9.5 已修复)和 activerecord-import(直至 0.2.7 已修复)中。
ar-extensions 用于 Rails 2.x。 activerecord-import 应该用于 Rails 3.x。它们支持相同的 API。
This bug existed in ar-extensions (up to 0.9.5 in which it was fixed) and activerecord-import (up to 0.2.7 in which it was fixed).
ar-extensions is used for Rails 2.x. activerecord-import should be used for Rails 3.x. They support the same APIs.
您可以在运行导入之前将导入项目的
id
列设置为 nil,以避免 Mass_assignment 问题:注意:您可能想查看此 gem 的新版本:https://github.com/zdennis/activerecord-import
You could set the
id
column to nil on the imported items before running the import to avoid the mass_assignment problem:Note: It looks like there's a new version of this gem you might want to look at: https://github.com/zdennis/activerecord-import
使用 activerecord-import 0.2.7 + Rails 3.0.7 时,但从外部 XML 文件导入数据时,我仍然会看到同样的错误。这是整个事情(作为 Rails 迁移;我不知道如何运行它):
编辑:没关系;问题是我明确尝试设置 ID 值。噢!
I still see this same error when using activerecord-import 0.2.7 + Rails 3.0.7, but when importing data from an external XML file. Here's the whole thing (as a Rails migration; I wasn't sure how else to run it):
EDIT: nevermind; the problem is that I AM explicitly trying to set the ID value. D'oh!