Implement array and iterate

+1 vote
asked Sep 12, 2017 by peter-penzov

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?

2 Answers

+4 votes
answered Sep 12, 2017 by ilya

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)
+2 votes
answered Sep 12, 2017 by sachin-singh

try this out:

  if args[:remove_existing_trxs] == 'true'
    [Acquirer, Company, Currency, AdminUser,
    BaseReseller, Terminal, Contract, Merchant,
    MerchantUser, PaymentTransaction].each(&:destroy_all)
  end
Welcome to Q&A, where you can ask questions and receive answers from other members of the community.
...