2015-04-07 87 views
0

我創建了一個腳本,用於在另一個數據庫中填充新表,因爲我將此表從一個數據庫(table_1)中移出並且到另一個數據庫(db_2)中。我已經在新的數據庫(db_2)中創建了'new_geo_fence'表,並希望讓腳本在下面運行以遷移數據。下面的腳本:Rails 4腳本+錯誤:將數據從一個表遷移到另一個

class NewGeoFence < ActiveRecord::Base 
attr_accessible :name, :address, :latitude, :longitude, :radius, :customer_id 

     belongs_to :customer, :foreign_key => 'customer_id' 
    end 

require 'rubygems' 

GeoFence.all.each do |g| 
    gf = NewGeoFence.new(
    :id => g.id, 
    :name => g.name, 
    :address => g.address, 
    :latitude => g.latitude, 
    :longitude => g.longitude, 
    :radius => g.radius,   
    :created_at => g.created_at, 
    :updated_at => g.updated_at, 
    :customer_id => g.customer_id 
) 
    gf.save 
end 

然而,當我運行它,我得到這個錯誤:

/activerecord-4.0.13/lib/active_record/attribute_assignment.rb:47:in `rescue in _assign_attribute': unknown attribute: customer_id (ActiveRecord::UnknownAttributeError) 

有什麼我錯過了得到這個腳本運行?

謝謝!

回答

0

你每次調用上時,你應該對象的數組上稱這是一個類,所以

GeoFence.all.each do |g| 
0

的Rails 4需要的參數做質量分配時要列入白名單。爲此,請使用強參數

GeoFence.all.each do |g| 
    params = ActionController::Parameters.new({ 
    geofence: { 
     id: g.id, 
     name: g.name, 
     address: g.address, 
     latitude: g.latitude, 
     longitude: g.longitude, 
     radius: g.radius,   
     created_at: g.created_at, 
     updated_at: g.updated_at, 
     customer_id: g.customer_id 
    } 
    }) 
    gf = NewGeoFence.new(params.require(:geofence).permit!) 
    gf.save 
end 
相關問題