Ruby on rails 这是否可以利用';activerecord导入&x27;用于Rails中无模型表的gem。?
我想将记录批量插入到没有模型的表中。我确实关注了链接。。除了“导入”命令外,一切正常。因为我没有模型 我无法为该表创建空模型。好吧,我告诉你,为什么我不能为它创建一个表。我正在使用IOS apns服务器进行推送通知功能。当我配置它时,它在我的数据库中创建了很多没有模型的表。在其中一个表中,我想在单个查询中批量插入记录。最初我是用loop做的。这影响了演出。所以,我想进行优化。无论解决方案是什么,请给我任何建议。以下是我的方法Ruby on rails 这是否可以利用';activerecord导入&x27;用于Rails中无模型表的gem。?,ruby-on-rails,ruby-on-rails-3,activerecord,activerecord-import,Ruby On Rails,Ruby On Rails 3,Activerecord,Activerecord Import,我想将记录批量插入到没有模型的表中。我确实关注了链接。。除了“导入”命令外,一切正常。因为我没有模型 我无法为该表创建空模型。好吧,我告诉你,为什么我不能为它创建一个表。我正在使用IOS apns服务器进行推送通知功能。当我配置它时,它在我的数据库中创建了很多没有模型的表。在其中一个表中,我想在单个查询中批量插入记录。最初我是用loop做的。这影响了演出。所以,我想进行优化。无论解决方案是什么,请给我任何建议。以下是我的方法 # Push Notification to all users of
# 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
#向应用程序的所有用户推送通知。
def ios_推送_通知(管理员notif)
批量_数据=[]
n=Rpush::Apns::Notification.new
n、 app=Rpush::Apns::app.find\u by\u name(“ios\u应用程序”)
ios\u user\u reg\u id=UserRegId.where(:device\u os=>“ios”).pull(:user\u gcm\u reg\u id)
ios_user_reg_id.每个do |设备_令牌|
n、 设备令牌=设备令牌
n、 alert=admin\u notif.try(:content)
n、 数据={foo::bar}
批量数据
您的表格方案未知,请在使用前调整列。嘿,Anatoly,谢谢您的回答。我实施了您的解决方案,效果很好。再次感谢。。
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