nSurface

preload and import a csv file into your rails 3 database

For a project I’m working on, there’s existing data that has to be in the database. The data exists in an excel spreadsheet which I then export as a CSV file. I then create the rake file lib/tasks/bootstrap.rake and place the following in it:

namespace :bootstrap do
      desc "populate database with data from data1.csv"
      task :default_data1 => :environment do
        lines = File.new('public/data/data1.csv').readlines
        header = lines.shift.strip
        keys = header.split(',')
        lines.each do |line|
          params = {}
          values = line.strip.split(',')
          keys.each_with_index do |key,i|
            params[key] = values[i]
          end
          Post.create(params)
        end
      end

      desc "populate database with data from data2.csv"
      task :default_data_2 => :environment do
        lines = File.new('public/data/data2.csv').readlines
        header = lines.shift.strip
        keys = header.split(',')
        lines.each do |line|
          params = {}
          values = line.strip.split(',')
          keys.each_with_index do |key,i|
            params[key] = values[i]
          end
          Post.create(params)
        end
      end

      desc "Run all bootstrapping tasks"
      task :all => [:default_data_1, :default_data_2]
    end

I’m uploading two CSV files that I’ve moved to public/data/. The data looks something like this:
[csv]
title,content,author
foo,lorem ipsum,john doe
bar,ipsum lorem,jane doe
[/csv]

and as long as the first line in the csv matches the fields in your model, they’ll slip right in.

finally, run rake bootstrap:all to make the magic happen!

credits: this code is a mashup of these two little code snippits: one two

proto? proto!


proto-proto-block, originally uploaded by neutralSurface.

If anyone has any idea what a “proto-block” is, or where to purchase one, let me know.

random rules


Sorry, you have a Java-challenged browser!

Here’s the other version of the tile game. It’s actually the first one I wrote and a lot more difficult to achieve logically. This other one can be completely random because the tiles have ends on each side, but in this version, only two sides have arc ends which constrains the options for rotation significantly as to avoid “dead ends.” Once I had written this code however, it was only a matter of about ten minutes to trim out the excess to arrive at the other version of this code which is conceptually simpler.

cul-de-sac


Sorry, you have a Java-challenged browser!

My critic showed us these children’s toys using tiles arranged in patterns. arranged manually, it takes some time to simply copy/paste/rotate/move so I decided to script the process. This applet shows no bias towards the two tiles, but it is possible to adjust a bias and achieve more predictable results. Each time the script is run, the results are unique (in this version, I believe there are 2^(50^2) permutations) Maybe I’ll build some sort of interface for this thing…or maybe I have better things to do.

tomorrow I’ll post another version with different pieces.