WARNING. You cannot assign these protected attributes: id (ar-extensions)

I am using Rails 2.3.5 and AR extensions 0.9.3

I am trying to perform a bulk insert from one table to another table located on another server / database. However, I do not want to rewrite anything. Just a simple insert at the end of the new table is good enough.

I noticed that I received this warning: WARNING: these protected attributes cannot be assigned: id

My old records are overwritten .. so how do I get around this?

Thank!

Edit: Figured this out. It seems like all I need is to define an array of attributes that I want (excluding id) and pass them to the import function.

Update:

tableA_items = TableA.find(:all)

TableB.establish_connection("other_server")
TableB.import tableA_items
+3
source share
3 answers

ar-extensions ( 0.9.5, ) activerecord-import ( 0.2.7, ).

ar-extensions Rails 2.x. activerecord-import Rails 3.x. API.

+1

id , _:

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

. , , : https://github.com/zdennis/activerecord-import

0

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

: nevermind; , AM ID. D'!

0

Source: https://habr.com/ru/post/1787582/


All Articles