Ruby on rails 如何在将文件上传的数据存储到数据库之前进行后处理?
Ruby on rails 如何在将文件上传的数据存储到数据库之前进行后处理?,ruby-on-rails,Ruby On Rails,我是新来的
从文件中获取数据后,我需要对其进行处理。
我认为有两种方法可以做到这一点
应用程序有3种型号:
Data
Segment - belongs to data
Point - belongs to Segment
第一种方法是:
创建类(
我是新来的
从文件中获取数据后,我需要对其进行处理。
我认为有两种方法可以做到这一点
应用程序有3种型号:
Data
Segment - belongs to data
Point - belongs to Segment
第一种方法是:
创建类(
将数据读取到对象(1中的类)
过程数据
将数据上载到数据库
这样,我有一个问题——定义类的最佳位置是什么?模型、/lib等
第二种方法是:
将数据读取到数据库
过程数据
数据库中的更新
这样我就有了另一个问题——如何在一次交易中更新一堆积分
你能帮我接近它吗
更新:
也许我应该读取散列数组中的文件数据,对其进行处理并上传到DB?只要Rails设计成web服务器,这将是我最好的方法:
routes.rb
post '/uploads/log/', to: 'logs#create'
uploads_controller.rb
def create
@upload = Upload.new(parsed_upload_file) #define a method to parse the filetype
if @upload.save
#actions after save like show the parsed result or whatever
else
#display errors like format issues or actions to perform
end
end
uploads.rb #model
class Upload << ActiveRecord::Base
attr_accessor #if you need attributes not defined in the db but in the file
before_create :pre_process_data
private
def pre_process_data
#actions to preprocess data
end
end
到目前为止,在这一点上有什么失败了。你将做什么样的处理?此操作需要多少时间?它将是“移动平均”和“中值”过滤器。对于数据中的一个对象,大约有5个段,每个段大约有100-150个点。我不知道服务器(1核2Ghz,1 Gb RAM,常规硬盘7200 rpm)的负载是否会很重。我想说的是,你应该使用一些nosql来存储传入的请求(如果格式允许的话,根本不需要任何处理),然后使用后台作业来处理它们。我不确定,但我会在谷歌上搜索一下。我想显示报告,并给予永久链接后,文件上传给用户。非常感谢!这比我的尝试要好得多。你能帮我解决这个问题吗?我如何在Rails 4.0.2中实现attr\u访问器
?在模型中:attr\u访问器:attribute1,:attribute2
def bunch_update
@invalid_points = []
points_array.each do |point|
new_point = Point.new(point)
@invalid_points << new_point unless new_point.valid?
end
points_array.each {|point| point.save} if @invalid_points.empty?
end
@invalid_points.each do |point|
point.errors.full_messages
end