这有可能使使用的ActiveRecord进口“的宝石在Rails的模型少表? [英] Is that Possible to make use of 'activerecord-import' gem for a Model-less table in Rails.?

查看:186
本文介绍了这有可能使使用的ActiveRecord进口“的宝石在Rails的模型少表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想批量插入的记录到一个表,其中没有一个模型。我没有按照链接<一个href="http://stackoverflow.com/questions/8505263/how-to-implement-bulk-insert-in-rails-3/8505300#8505300">How Rails中实现批量插入3 ..一切都很好,除了导入命令。因为我没有一个模型。

我不能创建该表的一个空模型。好吧,我告诉你,我为什么不能创建一个表的。我使用的IOS的APN服务器的推送通知功能。当我配置的,它创造了很多的表到无模式我的数据库。在这些表中的一个我想在一个查询批量插入的记录。起初,我做到了带环。它影响了性能。所以,我想有优化。无论哪种解决方案,请建议我任何东西。下面是我的方法。

 #推送通知的应用程序的所有用户。
  高清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 =&gt;中IOS)摘去。(:user_gcm_reg_id)
    ios_user_reg_ids.each做| device_token |
      n.device_token = device_token
      n.alert = admin_notif.try(:内容)
      n.data = {FOO:巴}
      bulk_data&LT;&LT; ñ
    结束
    p bulk_data
    RPUSH :: APNS :: Notification.import bulk_data#我在这里得到的错误,因为这种模式一点儿也不存在。
  结束
 

对不起,我英文不好。先谢谢了。

解决方案

 类RPUSH :: APNS ::通知
  高清self.import(bulk_data)
    SQL =

    直到bulk_data.empty?
      排= bulk_data.pop
      sql.push((#{row.device_token},{#} row.alert,#{row.data}))
    结束

    的ActiveRecord :: Base.connection.execute(INSERT INTO表(device_token,警报,数据)VALUES#{sql.join('')})
  结束
结束
 

您表的计划是不明所以使用前请调整列。

I want to bulk insert records into a table, which does not have a Model. I did follow the link How to implement bulk insert in Rails 3 .. Everything was fine except the 'import' command. Because I do not have a Model.

I can not create an empty Model for that table. Okay, I tell you, why I cant create a table for that. I am using IOS apns server for Push Notification feature. When I configured that, it created lot of tables into my database without Models. In one of these tables I want to bulk insert records in a single query. Initially I did it with loop. It affected the performance. So, I wanted to have optimization. Whatever the solution is, Please suggest me anything. The following is my method.

# 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

Sorry for my poor English. Thanks in advance.

解决方案

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

Your table scheme is unknown so please adjust columns before use.

这篇关于这有可能使使用的ActiveRecord进口“的宝石在Rails的模型少表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆