警告:无法批量分配这些受保护的属性:id(ar-extensions)

发布于 2024-10-14 05:43:42 字数 408 浏览 1 评论 0原文

我正在使用 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 技术交流群。

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

发布评论

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

评论(3

失而复得 2024-10-21 05:43:42

此错误存在于 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.

苏佲洛 2024-10-21 05:43:42

您可以在运行导入之前将导入项目的 id 列设置为 nil,以避免 Mass_assignment 问题:

tableA_items.each {|item| item.id=nil}

注意:您可能想查看此 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:

tableA_items.each {|item| item.id=nil}

Note: It looks like there's a new version of this gem you might want to look at: https://github.com/zdennis/activerecord-import

不及他 2024-10-21 05:43:42

使用 activerecord-import 0.2.7 + Rails 3.0.7 时,但从外部 XML 文件导入数据时,我仍然会看到同样的错误。这是整个事情(作为 Rails 迁移;我不知道如何运行它):

require 'open-uri'

artists = Array.new
Artist.establish_connection("http://localhost:3000")

begin
  open("*some-url*") do | artists_file |
    artists_file.each do | line |
      if line =~ /<artist id="([\w_]*)" name="(.*)"[ <]/
        puts $1, $2

        if line =~ / sort="(.*)"/
          puts $1
        end

        begin
          artist = Artist.new(:id => $1, :name => $2)
          artists << artist
        rescue
          puts "Couldn't add " + $1 + ": " + $!
        end
      end
    end

    Artist.import artists, :validate => true
  end
rescue
  puts "Couldn't open the artists file."
end

编辑:没关系;问题是我明确尝试设置 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):

require 'open-uri'

artists = Array.new
Artist.establish_connection("http://localhost:3000")

begin
  open("*some-url*") do | artists_file |
    artists_file.each do | line |
      if line =~ /<artist id="([\w_]*)" name="(.*)"[ <]/
        puts $1, $2

        if line =~ / sort="(.*)"/
          puts $1
        end

        begin
          artist = Artist.new(:id => $1, :name => $2)
          artists << artist
        rescue
          puts "Couldn't add " + $1 + ": " + $!
        end
      end
    end

    Artist.import artists, :validate => true
  end
rescue
  puts "Couldn't open the artists file."
end

EDIT: nevermind; the problem is that I AM explicitly trying to set the ID value. D'oh!

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