是否可以在Rails中为无模型表使用’activerecord-import’gem。

我想将记录批量插入到没有Model的表中。 我确实按照链接如何在Rails 3中实现批量插入 ..一切都很好,除了’import’命令。 因为我没有模特。

我无法为该表创建一个空模型。 好的,我告诉你,为什么我不能为此创建一个表。 我正在使用IOS apns服务器进行推送通知function。 当我配置它时,它在没有模型的情况下在我的数据库中创建了很多表。 在其中一个表中,我想在单个查询中批量插入记录。 最初我用循环做了。 它影响了性能。 所以,我想进行优化。 无论解决方案是什么,请建议我。 以下是我的方法。

# Push Notification to all users of the application. def ios_push_notification(admin_notif) bulk_data = [] n = Rpush::Apns::Notification.new n.app = Rpush::Apns::App.find_by_name("ios_app") ios_user_reg_ids = UserRegId.where(:device_os=>"ios").pluck(:user_gcm_reg_id) ios_user_reg_ids.each do |device_token| n.device_token = device_token n.alert = admin_notif.try(:content) n.data = { foo: :bar } bulk_data << n end p bulk_data Rpush::Apns::Notification.import bulk_data # I get error here, since this model does'nt exist. end 

抱歉我的英语不好。 提前致谢。

 class Rpush::Apns::Notification def self.import(bulk_data) sql = "" until bulk_data.empty? row = bulk_data.pop sql.push("(#{row.device_token}, #{row.alert}, #{row.data})") end ActiveRecord::Base.connection.execute("INSERT INTO table (device_token, alert, data) VALUES #{sql.join(',')}") end end 

您的餐桌计划未知,因此请在使用前调整栏目。