Insert problem with rails3

tran
=Transaction.select(“transactions.id,transactions.user_id,transactions.branch_id,transactions.customer_id,transactions.membership_type_id,transactions.bill_amount,transactions.bill_date”)
treport = TransactionReport.new
puts “from report”
tran.each do |u|
bs = TransactionReport.new(
:user_id => u.user_id,
:branch_id => u.branch_id,
:transaction_id => u.id,
:customer_id => u.customer_id,
:membership_type_id => u.membership_type_id,
:bill_amount => u.bill_amount,
:bill_date => u.bill_date
)
bs.save

Here the object creation will happen in loop so this will create new
object for TransactionReport with each insertion. How to avoid this in
rails3…?

Thank you
vishnu

On 1 February 2012 07:56, amvis [email protected] wrote:

:customer_id => u.customer_id,
:membership_type_id => u.membership_type_id,
:bill_amount => u.bill_amount,
:bill_date => u.bill_date
)
bs.save

Here the object creation will happen in loop so this will create new object
forTransactionReport with each insertion. How to avoid this in rails3…?

Exactly what is it that you are trying to avoid? If you don’t want it
to create new objects in the loop then just remove the code.

Colin

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…
Also i want to know… how to do the one time object creation for all
insertion in that code…?

Thank you
vishnu

On 1 February 2012 09:22, amvis [email protected] wrote:

Please don’t top post, it makes it difficult to follow the thread.
Insert your reply at appropriate points in previous message. Thanks.

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

Is that what you expect or is that part of the problem?

, but after i truncate the
tables

What do you mean by truncate the tables? Do you mean you have removed
all the rows?

, again when i execute that operation, the row count starting from
173

If there are still records in the database then you have not removed
them.

… i think that happens of new object creation for each transaction…
Also i want to know… how to do the one time object creation for all
insertion in that code…?

Sorry, I still don’t understand exactly what you want. Please try and
write very carefully a bit at a time exactly what happens and what you
are want to happen.

Colin

On Wed, Feb 1, 2012 at 10:22 AM, amvis [email protected] 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

On 1 February 2012 11:07, amvis [email protected] wrote:

Colin simply i am saying here the truncate means, remove all the rows. so
after the truncate, it should start from id 1.
But now the default id start from the next to old one( that means
173,174…).

Yes, that is the way it works. If you want anything different I
think you will have to put a column in the database (sequence for
example) and manage it yourself. Though I think it may be possible to
override the numbering system but I would not recommend it.

Did you not understand my request about top posting?

Colin

What do you mean by truncate the tables? Do you mean you have removed

Sorry, I still don’t understand exactly what you want. Please try and
write very carefully a bit at a time exactly what happens and what you
are want to happen.

Colin


gplus.to/clanlaw