Project

uploadable

0.0
No commit activity in last 3 years
No release in over 3 years
Upload your csv to activerecord model
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Runtime

 Project Readme

Build Status

Uploadable - A simple gem, with custom options to upload a csv to a model

How to use?

  1. How to make a model uploadable
  • use keyword uploadable and mandatory_fields & optional_fields

  • for setting up the fields which the gem should read and upload to model

  • any other field would be ignored

        class Car < ActiveRecord::Base  
          uploadable :mandatory_fields => [:company, :model], 
                 :optional_fields => [:price, :category]
    
          validates_presence_of :company, :model
          validates_numericality_of :price, :greater_than => 100000
          validates_inclusion_of :category, :in => %w(two three four)
        end
    
  1. Using csv partial upload : upload_from_csv
  • You can use function upload_from_csv which will upload records without errors

        Car.upload_from_csv "Company,Model,Price,Category\n,800,250000,four\nHonda,City,8000000,four"
    
  • Output -

        [
         #<Car id: nil, company: nil, model: "800", price: 250000, category: "four", created_at: nil, updated_at: nil>, 
         #<Car id: 27, company: "Honda", model: "City", price: 8000000, category: "four", created_at: "2012-07-01 10:32:04", updated_at: "2012-07-01 10:32:04">
        ]
    
  1. Using transactional upload : upload_from_csv!

          Car.upload_from_csv! "Company,Model,Price,Category\n,800,250000,four\nHonda,City,8000000,four"
    

    if any record contains error, it wouldn't upload, else would upload all returns you the array of objects (uploaded or unuploaded)

Note: You can use errors method and iterate over the list of objects to collect errors