Saturday, 15 September 2012

ruby on rails - How to make postprocessing data uploaded from file before store in DB? -



ruby on rails - How to make postprocessing data uploaded from file before store in DB? -

i'm new in ror.

i need process info after file. see 2 way done it.

application have 3 models:

data segment - belongs info point - belongs segment

first way is:

make classes (< object) , define same fields in models read info objects (classes in 1.) process info upload info data base

in way have 1 question - best place define classes? model, /lib, etc?

second way is:

read info db process data update in db

in way have question - how update bunch of points in 1 transaction?

could please help me approach it?

update: may should read file info in array of hashes, process , upload db?

as long rails designed work web server best approach:

routes.rb post '/uploads/log/', to: 'logs#create' uploads_controller.rb def create @upload = upload.new(parsed_upload_file) #define method parse filetype if @upload.save #actions after save show parsed result or whatever else #display errors format issues or actions perform end end uploads.rb #model class upload << activerecord::base attr_accessor #if need attributes not defined in db in file before_create :pre_process_data private def pre_process_data #actions preprocess info end end

and define form upload fields or url.

i approach long have nice inteface upload posts. can define urls them, or cron automatic request file url.

and other question, dislike transactions long might need know 1 failed. rather recommend this:

def bunch_update @invalid_points = [] points_array.each |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

with able utilize in view:

@invalid_points.each |point| point.errors.full_messages end

to failed on points.

ruby-on-rails

No comments:

Post a Comment