count_by_sql("SELECT COUNT(*)
FROM paypal_transactions
WHERE transaction_id
='" <<
line[cols[
'Transaction ID'.columnize
] ] <<
CHAPTER 7 n TRACKING EXPENDITURES WITH PAYPAL 143
"';")==0
PaypalTransaction.new do |transaction|
cols.each do |field_name, field_position|
transaction.send("#{field_name }=", line[field_position])
end
transaction.save
end
end
end
end
The call to count_by_sql checks if a given transaction_id has already been entered; if
it has, then the transaction won??™t be reentered. (Of course, in theory, the same file won??™t
be entered twice, but if the user does enter the same file twice, you won??™t get any duplicate
results.)
The PaypalTransaction.new method creates a new transaction, and then the code
loops through each column, and uses the send method to set each column to its appropriate
value. send is a Ruby method that allows you to dynamically call methods on a
given object. It takes a string with the method name and some arguments, and calls
that method with the specified arguments.
Pages:
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212