Ruby on Rails Wednesday, February 1, 2012

On Wed, Feb 1, 2012 at 10:22 AM, amvis <vgrkrishnan@gmail.com> wrote:
Thanks for the reply.

Exactly what  going on here is now,after the  execution of one insertion some amount of data will insert, around 172 rows, but after i truncate the tables, again when i execute that operation, the row count starting from 173... i think that happens of new object creation for each transaction...

It has to do with the sequence that is set on the primary key. Why does that matter? The
id is just a number that is sequential (that is "unique" and "monotonic" upwards, it is not
even continuous ... upon failed inserts, a "sequence number" will be consumed and
"lost forever", but ... why care).

If you really wanted something like "line numbers" in your report that have meaning in
the real business context, than you should make them yourself as separate column
(and not abuse the database sequence for that)

 
Also i  want to know... how to do the one time object creation for all insertion in that code...?

Please explain again what is the problem...


Also, a few remarks about naming:

* "Transaction" is a dangerous word to use in the context of databases
   (I presume you intend it for FinancialTransaction but in database terms, it has a special meaning)

* tran is a poor name for "transactions". It is a "list" (Enumerable), so use a name
  that represent that (a plural form, "trans" or "input_transactions" or ...).

* The class  TransactionReport really has the wrong name ... What you are making inside
  the loop is a TransactionReportLine or TransactionReportEntry and that whole list of
  TransactionReportLines _together_, make for a TransactionReport. Such a
  TransactionReport will have separate info, such as:
  * data create (created_at)
  * by who was the report ran
  * for which context (all FinancialTransaction, or only the one's in one currency, one branch etc).

* and then if you have TransactionReport and TransactionReportLine, you could even do

class TransactionReport
  has_many :transaction_report_lines, :autosave => true
end

tr = TransactionReport.new(:creator => "Shawn", :period => "Jan 2012")

input_transactions.each do |input_transaction|
  # this will NOT save to the db
  tr.transaction_report_lines.build(input_transaction.attributes.slice(:user_id, ...)) # not tested
end

tr.save! # this will save all at once, or nothing, if that was your intention

Code not tested, but indicative of a different style.

HTH,

Peter

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

No comments:

Post a Comment