我想将记录大容量插入到没有模型的表中。我确实遵循了链接How to implement bulk insert in Rails 3 ..。除了“导入”命令之外,一切都很好。因为我没有模特。
我不能为那个表创建一个空模型。好吧,我告诉你,为什么我不能为此创建一个桌子。我正在使用IOS apns服务器的推送通知功能。当我配置它时,它在我的数据库中创建了很多没有模型的表。在其中一个表中,我希望在单个查询中批量插入记录。一开始我是用循环来做的。它影响了演出。所以,我想进行优化。不管解决办法是什么,请给我任何建议。以下是我的方法。
# 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为我糟糕的英语感到抱歉。提前谢谢。
发布于 2015-07-25 14:57:33
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您的表方案未知,请在使用前调整列。
https://stackoverflow.com/questions/31612059
复制相似问题