In my testing database, I noticed that I had imported payrolls 3 times.
Payrolls also have many line items per payroll, so it was going to be a lot of
work to remove the dups.
Found this ruby snippet (this was a ruby project)
a = Payroll.find(:all)
b = Hash[ *a.map { |x| [x[:contract_id].to_s + x[:reporting_date].to_s,
x]}.flatten].values
c=a-b
c.each {|x| x.destroy}
so, what this code is doing line by line is:
1) getting all the payroll records
2) this is a biggie - iterate through all the records making a hash structure
with a key created by appending reporting_date to contract_id and a value of
the record itself, then assigning b all of those values
3) get the set difference between a and b
4) iterate over the difference, calling the destroy method on the duplicates --
which in turn calls destroy on all child objects.
problem solved!
that's probably the most perl-esque ruby code I've ever used, but it is pretty
damn cool.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
Order the Adobe Coldfusion Anthology now!
http://www.amazon.com/Adobe-Coldfusion-Anthology-Michael-Dinowitz/dp/1430272155/?tag=houseoffusion
Archive:
http://www.houseoffusion.com/groups/cf-community/message.cfm/messageid:324185
Subscription: http://www.houseoffusion.com/groups/cf-community/subscribe.cfm
Unsubscribe: http://www.houseoffusion.com/groups/cf-community/unsubscribe.cfm