Saturday, May 31, 2008

2. Creating the csv_test app

So for the app... I am going to call it csv_test so I get going:

>rails --d mysql csv_test
# I specify mysql bc the mac keeps creating sqlite and I like mysql better (i think)


#stuff gets created

>rake db:create

#this creates my databases...

>ruby script/generate controller import

#creates my controller

>ruby script/generate model transaction

#I create a transaction model mainly because I want to create my transaction table this gives me the file : 001_create_transactions. I open this and add the following items for my table:

class CreateTransactions < ActiveRecord::Migration
def self.up
create_table :transactions do |t|
t.datetime :date
t.integer :amount
t.integer :card_id

t.timestamps
end
end

def self.down
drop_table :transactions
end
end

#So its a simple table that has three columns :date, :amount, :card_id. These will hopefully correspond to my csv file I am trying to import.


Here are the contents of my data file - test.csv:

date, amount, card_id
5/06/2008, 20, 1234
06/05/2008, 50, 1235
10/30/2008, 45.45, 1342

I have a date, amount and a card_id just like my database table. Pretty simple.

No comments: