Deliver value; what are the benefits for people using the end product, does it improve a persons life?
Although "improve a person's life" is subjective, its a good question to ask - not yourself - them. We can imagine how a person life would be improved if they just did this or that but to them the mountain may be too high or the forest too thick. So the first step is ask them and if they say yes it would - celebrate. If they are unsure then you have to go back and think again. Why can't they see it? Could it be you are wrong? Maybe you can cut some trees down or give them some mountain boots to help them - but careful fearless social innovator - be sure when you step back or out of the situation they don't just fall or get lost in trees - it has to be sustained value....
Tuesday, March 9, 2010
Saturday, May 31, 2008
transaction.records.create!(row.to_hash)
This one I really like:
transactions.records.create!(row.to_hash)
But now I have a problem. Is this where I indicate what table I want my csv file imported to? I used the word transactions and maybe should use a different one not to be confused with the .transaction class method.
Note my table name is plural... Should I use the plural here?
(row.to_hash)
James told me this magic allows me not to have to specify a hash for all my columns that I want to import into my database table. If I did not have this I would have to do this:
transactions.records.create!(:date => date, :amount => amount, :card_id => card_id
If I had a ton of columns this would hard to do... Also James said that in order for this row.to_hash to work you need to have headers set to true or specify the headers in the block or it will complain.
transactions.records.create!(row.to_hash)
But now I have a problem. Is this where I indicate what table I want my csv file imported to? I used the word transactions and maybe should use a different one not to be confused with the .transaction class method.
Note my table name is plural... Should I use the plural here?
(row.to_hash)
James told me this magic allows me not to have to specify a hash for all my columns that I want to import into my database table. If I did not have this I would have to do this:
transactions.records.create!(:date => date, :amount => amount, :card_id => card_id
If I had a ton of columns this would hard to do... Also James said that in order for this row.to_hash to work you need to have headers set to true or specify the headers in the block or it will complain.
:headers => true,
This one I get...
:headers => true,
This tells the FasterCSV that the file you are uploading has headers. So that answers one of the questions. If you don't have headers it is :headers => false
:headers => true,
This tells the FasterCSV that the file you are uploading has headers. So that answers one of the questions. If you don't have headers it is :headers => false
FasterCSV.foreach...
This is the FasterCSV statement
FasterCSV.foreach("csv_test/public/csv/test_copy.csv",
for reading the file line by line. You can read about it here:
http://fastercsv.rubyforge.org/
I decided to start simple because I was having trouble understanding the code to make the import work from HTML. Once I get this to work I will work on getting the import file to work.
FasterCSV.foreach("csv_test/public/csv/test_copy.csv",
for reading the file line by line. You can read about it here:
http://fastercsv.rubyforge.org/
I decided to start simple because I was having trouble understanding the code to make the import work from HTML. Once I get this to work I will work on getting the import file to work.
begin
Now for the begin statement:
begin
Record.transaction do
The Record.transaction do is nice and descriptive and tells the app that we want to record a transaction... right? Well, I would think so... breaking this up we see that Record is a class (because it starts with a Capitalization... ) But I have not defined Record anywhere so it must be a built in class in Ruby... I have searched for it but cannot find it... any help here?
The .transaction I did find out and is explained pretty well here:
http://api.rubyonrails.org/
by going to the file:
vendor/rails/activerecord/lib/active_record/transactions.rb
in a nutshell we are using .transaction because we want the insert to our database to be done only if the whole insertion succeeds. Thats why transaction is called a protective block... If it does not succeed then we can role the transaction back... no harm.
begin
Record.transaction do
The Record.transaction do is nice and descriptive and tells the app that we want to record a transaction... right? Well, I would think so... breaking this up we see that Record is a class (because it starts with a Capitalization... ) But I have not defined Record anywhere so it must be a built in class in Ruby... I have searched for it but cannot find it... any help here?
The .transaction I did find out and is explained pretty well here:
http://api.rubyonrails.org/
by going to the file:
vendor/rails/activerecord/lib/active_record/transactions.rb
in a nutshell we are using .transaction because we want the insert to our database to be done only if the whole insertion succeeds. Thats why transaction is called a protective block... If it does not succeed then we can role the transaction back... no harm.
3. Import Controller code (FasterCSV)
So now for the real code.
The FasterCSV code I received from emailing James Edward Gray II who was nice enough to get me started on working on this...
I am going to break this code apart in bits in subsequent posts and hopefully get a better understanding about what is going on with comments from others...
Here is my first attempt:
class ImportController < ApplicationController
require 'fastercsv'
def index
end
def csv_import
begin
Record.transaction do
FasterCSV.foreach("csv_test/public/csv/test_copy.csv",
:headers => true)
do |row|
transactions.records.create!(row.to_hash)
end
end
flash[:notice] = "Successfully added."
redirect_to 'index'
rescue
#do something with the error
flash[:error] = "error adding. Please try again."
redirect_to '/import/index'
end
end
end
The FasterCSV code I received from emailing James Edward Gray II who was nice enough to get me started on working on this...
I am going to break this code apart in bits in subsequent posts and hopefully get a better understanding about what is going on with comments from others...
Here is my first attempt:
class ImportController < ApplicationController
require 'fastercsv'
def index
end
def csv_import
begin
Record.transaction do
FasterCSV.foreach("csv_test/public/csv/test_copy.csv",
:headers => true)
do |row|
transactions.records.create!(row.to_hash)
end
end
flash[:notice] = "Successfully added."
redirect_to 'index'
rescue
#do something with the error
flash[:error] = "error adding. Please try again."
redirect_to '/import/index'
end
end
end
Subscribe to:
Posts (Atom)