Hello all, I was wondering how to update an existing table column when reading in a CSV? I''m using FasterCSV and my controller method is this... - Import Controller - def process_csv # set file name file = params[:import][:file] rowcount = 0 Import.transaction do FasterCSV.parse(file, :headers => true, :header_converters => :symbol ) do |row| Import.create!(row.to_hash) rowcount += 1 end end # if successful then display, then redirect to index page flash[:notice] = "Successfully added #{rowcount} project(s)." redirect_to :action => :index rescue => exception file_name = params[:import][''file''].original_filename file_parts = params[:import][''file''].original_filename.split(''.'') ext = file_parts[1] if ext != ''csv'' error = "CSV file is required" else error = ERB::Util.h(exception.to_s) # get the error and HTML escape it end # If an exception in thrown, the transaction rolls back and we end up in this # rescue block flash[:error] = "Error adding projects to Import table. (#{error}). Please try again. " redirect_to :action => :new end As I read the CSV file, I wish to update a column in the model, mainly "ProjectType" as the record is being created. The CSV file does not have this information and there''s no possibility that it''ll ever have it. Thank you for any help. JohnM -- Posted via http://www.ruby-forum.com/.
Hey John, Last week I wrote this code to allow you to easily create models/ records using FasterCSV... it doesn''t directly answer your question, but it might help or at least give you some ideas: http://pastie.org/676681 If it looks this would be useful to you or others I can post it on github; I also have a test suite for it. - pat On Oct 29, 12:52 pm, John Mcleod <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Hello all, > I was wondering how to update an existing table column when reading in a > CSV? I''m using FasterCSV and my controller method is this... > > - Import Controller - > def process_csv > > # set file name > file = params[:import][:file] > rowcount = 0 > > Import.transaction do > FasterCSV.parse(file, > :headers => true, > :header_converters => :symbol ) do |row| > Import.create!(row.to_hash) > rowcount += 1 > end > end > # if successful then display, then redirect to index page > flash[:notice] = "Successfully added #{rowcount} project(s)." > redirect_to :action => :index > > rescue => exception > file_name = params[:import][''file''].original_filename > file_parts = params[:import][''file''].original_filename.split(''.'') > ext = file_parts[1] > > if ext != ''csv'' > error = "CSV file is required" > else > error = ERB::Util.h(exception.to_s) # get the error and HTML > escape it > end > # If an exception in thrown, the transaction rolls back and we end > up in this > # rescue block > > flash[:error] = "Error adding projects to Import table. (#{error}). > Please try again. " > > redirect_to :action => :new > end > > As I read the CSV file, I wish to update a column in the model, mainly > "ProjectType" as the record is being created. > > The CSV file does not have this information and there''s no possibility > that it''ll ever have it. > > Thank you for any help. > > JohnM > -- > Posted viahttp://www.ruby-forum.com/.
Sorry... forgot to mention the way it works is: - the first row of the CSV is taken as a list of column names - the remaining csv rows are data for new models - if your model contains a column/attribute matching one of the csv column names, it will be loaded - each new record is yielded to a block provided. On Oct 30, 9:45 am, Pat Shaughnessy <p...-TqfKzuDnBR2xH1ktNt+OzKxOck334EZe@public.gmane.org> wrote:> Hey John, > > Last week I wrote this code to allow you to easily create models/ > records using FasterCSV... it doesn''t directly answer your question, > but it might help or at least give you some ideas:http://pastie.org/676681 > > If it looks this would be useful to you or others I can post it on > github; I also have a test suite for it. - pat > > On Oct 29, 12:52 pm, John Mcleod <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> > wrote: > > > > > Hello all, > > I was wondering how to update an existing table column when reading in a > > CSV? I''m using FasterCSV and my controller method is this... > > > - Import Controller - > > def process_csv > > > # set file name > > file = params[:import][:file] > > rowcount = 0 > > > Import.transaction do > > FasterCSV.parse(file, > > :headers => true, > > :header_converters => :symbol ) do |row| > > Import.create!(row.to_hash) > > rowcount += 1 > > end > > end > > # if successful then display, then redirect to index page > > flash[:notice] = "Successfully added #{rowcount} project(s)." > > redirect_to :action => :index > > > rescue => exception > > file_name = params[:import][''file''].original_filename > > file_parts = params[:import][''file''].original_filename.split(''.'') > > ext = file_parts[1] > > > if ext != ''csv'' > > error = "CSV file is required" > > else > > error = ERB::Util.h(exception.to_s) # get the error and HTML > > escape it > > end > > # If an exception in thrown, the transaction rolls back and we end > > up in this > > # rescue block > > > flash[:error] = "Error adding projects to Import table. (#{error}). > > Please try again. " > > > redirect_to :action => :new > > end > > > As I read the CSV file, I wish to update a column in the model, mainly > > "ProjectType" as the record is being created. > > > The CSV file does not have this information and there''s no possibility > > that it''ll ever have it. > > > Thank you for any help. > > > JohnM > > -- > > Posted viahttp://www.ruby-forum.com/.