Peter Penzov
Peter Penzov

Reputation: 1658

Implement array and iterate

I have this Ruby code that I want to use:

if args[:remove_existing_trxs] == 'true'
    Acquirer.delete_all
    Company.delete_all
    Currency.delete_all
    AdminUser.delete_all
    BaseReseller.delete_all
    Terminal.delete_all
    Contract.delete_all
    Merchant.delete_all
    MerchantUser.delete_all
    PaymentTransaction.delete_all
  end

How can I define it as an array and iterate?

Upvotes: 1

Views: 69

Answers (2)

Ilya
Ilya

Reputation: 13477

Something like that?

[Model1, Model2].each do |model|
  model.public_send(:delete_all)
end 

Or with using Symbol#to_proc:

[Model1, Model2].each(&:delete_all)

Upvotes: 4

Sachin Singh
Sachin Singh

Reputation: 7225

try this out:

  if args[:remove_existing_trxs] == 'true'
    [Acquirer, Company, Currency, AdminUser,
    BaseReseller, Terminal, Contract, Merchant,
    MerchantUser, PaymentTransaction].each(&:destroy_all)
  end

Upvotes: 2

Related Questions