How to create a blob to a file content and save it to postgre sql using ruby - ruby-on-rails

Hi um facing an issue of creating a blob to a file and save it to a binary column in a postgre sql using rails . Still dont have an idea of how to start it. I would be happy if any one can tell me a way to do it.
file.each_line do |line|
line = Iconv.conv('utf-8', 'ISO-8859-1', line)
I want to save a file as Binary data (as a binary large object) and the file contains string

if you are looking for a gem that does this, carrier-wave should be helpflul: https://github.com/diogob/carrierwave-postgresql

Related

CSV read breaking when last line is \n for some reason

I'm working on a project with some supplied CSV files that I need to parse and do some manipulation on. One is throwing this error when I try to load it into a file using CSV.read('path/file.csv')
CSV::MalformedCSVError: Unquoted fields do not allow \r or \n (line 7911).
Now when looking at the file, the last line is just blank. It's a \n character. I feel like this should not break the CSV read but it is. Now, I could just check the end of the CSV documents and strip any access return carriages/new lines since that seems like it'll work but it doesn't seem like the correct way. Anybody have some advice?
Edit: Using Ruby 2.0.0 and Rails 4.0.5

Modify a csv file with a Ruby script

I have a .xlsx file converted to .csv.I need to write a script to modify this file(change/rename columns etc.) How can I open this .csv file and save it from within the script?
Thanks!
Open the csv file just like you would open any other file in ruby using the standard File api
csv_file = File.open('data.csv', 'r')
Parse it manually or use a library like FasterCSV. Make your modifications, writeback to the file and close. There is nothing inherently special about a csv file, work with it like you would with any file in ruby.
You should proably work with a CSV library (or in the ruby world a gem). So install the gem,
and your code will look something like this:
FasterCSV.foreach("path/to/file.csv") do |row|
# use row here...
end
http://fastercsv.rubyforge.org/
As far as I know, you cannot make inline modifications to the CSV file. You would have to output via another file.

How would you implement a db table of a list of all US zip codes in a rails application?

How would migrations be involved? Would you load them into the mysql db directly or have a ruby routine doing this.
To get the raw data I'd simply search on Google, you'll find lots of databases of zip codes, some of them are not free though.
Then I'd look at the data to get a clue on what columns I should include in the table, and build an appropriate migration and model.
Then I'd write an external ruby script which reads the data from whatever format the zip code database is in and writes it directly into the app's database. You could also do that as part of your Rails application, but I usually don't consider it necessary when dealing with external data only.
It's important, however, that the zip code table is not referenced by ID in some other table, since that makes it really complicated if you want to update it later (zip codes change). So I'd still store the zip code itself in, say, the user table, or wherever.
Here is a CSV link to all the zipcodes in the US: here . This file has 7 columns for each zipcode, with the zipcode being in the first column.
Now you could use the ruby CSV parser, or something like FasterCSV to parse the CSV, but I think it would be much faster if you simply parsed the CSV using a shell command. For example, I just ran this on my system and it instantly parses the file correctly:
cut -d ',' -f 1 zip_codes.csv > out.csv
At this point, it's a simple matter of reading in the file line by line in ruby like so:
File.open( out.csv ).each do |line|
Zipcode.create(:zip => line)
end
You will replace Zipcode.create.. with whatever model you are using, since you probably do not need a seperate Zipcode model.

rails reading lines of file before upload with paperclip

I am trying to upload a file in rails (using paperclip), and I want to process some of the file data before letting paperclip send it off to s3 storage. In my controller, I just grab the file parameter (which does give me a file) and then I try to read the lines into an array
csv_file = params[:activity][:data]
array = IO.readlines(csv_file.path)
The problem is, I'm only getting the last line of the file. I tried using .rewind, but still get just the last line.
I dislike readlines and I always use regular expressions. Try this.
End of line - \n
Handy block structure to ensure that the file handle is closed:
File.open(csv_file.path) do |f|
a = f.readlines
process a...
end
Reading a whole file into memory might not be a good idea depending on the size of the files.

Issues reading CSV file using OLEDB when filename have period

Issues reading CSV file using OLEDB when filename have period.
I have a code in C# that reads CSV File using OleDBProvider. It works perfect with filenames in regular format such as Budget.csv but failed when i renamed the file into Budget.DKK.csv or Budget.USD.csv
I throws this exception:
he Microsoft Jet database engine could not find the object 'Budget.DKK.csv'. Make sure the object exists and that you spell its name and the path name correctly.
I have no idea so far why is this happening.
If this thread is to be believed, then it is a known problem that won't be fixed. It mentions a work-around that allows the name to be forced into the old style 8.3 format.
And just as a random suggestion if you haven't tried it, maybe delimit the filename with brackets [filename.stuff.txt]. I doubt it is that simple, though.

Resources